Hi,
Been having real problems listing members of an AD group. The examples I've found online either don't work either or are a bit beyond my humble skills!
I can get members of simple groups easily enough, but teh code I'm using returns nothing at all if a group contains another group within it.
Does anyone have any ideas where I am going wrong? Or some examples of working code?
Here's what I have, which as stated only returns values if group doesn't contain subgroups (ssing VB2010)
Many thanks - Adam.
Sub ListMembers(ByVal GRP As String)
Dim GroupSearcher As New DirectorySearcher
Dim GroupSearchRoot As New DirectoryEntry(ldapserver, vusername, vpassword)
GroupSearcher.SearchRoot = GroupSearchRoot
GroupSearcher.Filter = "(&(ObjectClass=Group)(CN=" & GRP & "))"
Dim Result As SearchResult
Result = GroupSearcher.FindOne
GroupSearchRoot = New DirectoryEntry(Result.Path)
Call GetGroupMembers(GroupSearchRoot)
End Sub
Function GetGroupMembers(ByVal Entry As DirectoryEntry) As String
Dim groupMembers() As String
Dim adObject As DirectoryEntry
For Each member As Object In CType(Entry.Invoke("members", Nothing), System.Collections.IEnumerable)
adObject = New DirectoryEntry(member)
If adObject.SchemaClassName = "group" Then
For Each groupMember As String In GetGroupMembers(adObject)
If Not groupMembers.Contains(groupMember) Then
DataGridView1.Rows.Add(adObject.Properties("sAMAccountName").Value.ToString)
End If
Next
Else
DataGridView1.Rows.Add(adObject.Properties("sAMAccountName").Value.ToString)
End If
Next
End Function