1

私のプログラムでは、グループ内のユーザー ログオン名のリストを取得する必要があります。

これは私がこれまでに持っているものですが、すべてのユーザーのみを返します...私が名前を持っているグループ内のユーザーに切り詰める必要があります。

Option Explicit On
Imports System.DirectoryServices
Imports System.DirectoryServices.ActiveDirectory

Module Module1
    Sub Main()
        Dim ADEntry As New DirectoryServices.DirectoryEntry("LDAP://OU=Users,OU=Irvine,OU=KNS,DC=corp,DC=kns,DC=com")
        Dim objSearch As New System.DirectoryServices.DirectorySearcher(ADEntry)

        Dim oResults As DirectoryServices.SearchResultCollection
        Dim oResult As DirectoryServices.SearchResult

        '  THIS DOESNT WORK
        '  objSearch.Filter = "department = engineering"

        oResults = objSearch.FindAll

        For Each oResult In oResults
            Console.WriteLine(oResult.GetDirectoryEntry.Properties("sAMAccountName").Value)
        Next
    End Sub
End Module
4

4 に答える 4

1

グループのすべてのメンバーが必要な場合は、次のことを試してください。

1)グループにバインドします。

DirectoryEntry theGroup = 
   new DirectoryEntry("LDAP://cn=YourGroupname,ou=SomeOU,dc=YourCompany,dc=com");

2)次に、そのメンバーを列挙します。これは、グループの「メンバー」プロパティですDirectoryEntry

foreach(object dn in theGroup.Properties["member"])
{
   Console.WriteLine(dn);
}

グループの「メンバー」プロパティの各エントリは、そのメンバー(ユーザーまたは他のグループ)の完全なDN(識別名)である必要があります。

あなたの質問は、グループのメンバーを列挙しようとしていると言っていますが、コードはOU(組織単位)内のすべてを列挙しようとしているように見えます-これら2つのタスクはまったく異なります!本当に必要なのはどれですか?

Visual Basic.NETコードサンプルのクイックリストはMSDNライブラリにあります。または、CodeProjectのActive Directoryでほとんどすべてを実行する方法(C#サンプルを使用)について詳しく知ることができます。

マーク

于 2009-12-08T21:48:47.973 に答える
1

ファイラーをに変更してみてください

objSearch.Filter = "(&(objectCategory=user)(memberOf=CN=Employees,OU=Security Groups,DC=yourdomain,DC=com))"

グループは従業員です。

出典 : LDAP 検索フィルターの書き方

注:これをテストできませんでした。うまくいくかどうか教えてください。

于 2009-12-08T18:28:09.843 に答える
0

何年も前に、このタスクのためだけに頻繁に使用する AD コンポーネントを作成しました。これを試して。

Public Function GetUsersInGroup(ByVal GroupName As String) As String()
        If GroupName = String.Empty Then Return Nothing
        Dim Users() As String = Nothing
        Dim S As String = "LDAP://DC=YourCompany,DC=com"
        Dim Parent As New DirectoryServices.DirectoryEntry(S)
        Dim Search As New DirectoryServices.DirectorySearcher(Parent)

        Search.SearchScope = DirectoryServices.SearchScope.Subtree
        Search.Filter = "(CN=" & GroupName & ")"
        Search.PropertiesToLoad.Add("member")

        Dim Result As DirectoryServices.SearchResult = Search.FindOne
        Dim prop_value As String, i As Integer = 0
        If Result IsNot Nothing Then
            If Result.Properties("member").Count > 0 Then
                ReDim Users(Result.Properties("member").Count - 1)
                For Each prop_value In Result.Properties("member")
                    Dim S2 As New DirectoryServices.DirectorySearcher(Parent)
                    S2.SearchScope = DirectoryServices.SearchScope.Subtree
                    S2.Filter = "(" & prop_value.Substring(0, prop_value.IndexOf(","c)) & ")"
                    S2.PropertiesToLoad.Add("SAMAccountName")
                    Dim R2 As DirectoryServices.SearchResult = S2.FindOne
                    For Each Prop As String In R2.Properties("SAMAccountName")
                        Users(i) = Prop.ToUpper
                        i = i + 1
                    Next
                Next
                Exit For
            End If
        End If
End Function

検索する場所がわかっていれば、AD から多くの情報を引き出すことができます。

于 2009-12-10T17:18:32.487 に答える
0
    Dim ADEntry As New DirectoryServices.DirectoryEntry("LDAP://ou=users,ou=irvine,ou=kns,dc=corp,dc=kns,dc=com")
    Dim objSearch As New System.DirectoryServices.DirectorySearcher(ADEntry)

    Dim oResults As DirectoryServices.SearchResultCollection
    Dim oResult As DirectoryServices.SearchResult

    objSearch.Filter = "(&(objectCategory=person)(objectClass=user)(department=Engineering)(!userAccountControl:1.2.840.113556.1.4.803:=2))"
    oResults = objSearch.FindAll

    For Each oResult In oResults
        Console.WriteLine(oResult.GetDirectoryEntry.Properties("sAMAccountName").Value)
    Next

これはうまくいきました!!

于 2009-12-10T00:04:04.367 に答える