2010-05-25 141 views
1

我正在使用以下代碼示例獲取指定AD組中所有用戶的列表(本例中爲「域用戶」組中的所有用戶)。我列出的代碼效果很好,但有一個例外:它不會返回將其主要組設置爲「域用戶」的用戶。我如何獲得組中所有用戶的列表,包括將其設置爲主組的所有用戶列表?獲取Active Directory組中的所有用戶

Private Sub GetUsers() 

    Dim groupSearcher As New DirectorySearcher 
    Dim groupSearchRoot As New DirectoryEntry("LDAP://OU=Users,DC=domain,DC=com") 

    With groupSearcher 
     .SearchRoot = groupSearchRoot 
     .Filter = "(&(ObjectClass=Group)(CN=Domain Users))" 
    End With 

    Dim members As Object 
    members = groupSearcher.FindOne.GetDirectoryEntry.Invoke("Members", Nothing) 

    For Each member As Object In CType(members, IEnumerable) 
     Console.WriteLine(New DirectoryEntry(member).Name.Remove(0, 3)) 
    Next 
End Sub 

回答

4

解決方案使用DirectoryServices.AccountManagement找到代替:

For Each group As GroupPrincipal In UserPrincipal.FindByIdentity(New PrincipalContext(ContextType.Domain, "domain.com"), IdentityType.SamAccountName, "userName").GetGroups() 
    ' Do something with group name. 
Next 
0

我正在尋找一個強大的方法來做到這一點。如果您特別希望屬於「域管理員」的用戶,請查詢primaryGroupID = 512(512是知名ID,意思是「域管理員」)的所有用戶。

相關問題