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

OK - think I've sorted it...

Simplified the function to:

 Function GetGroupMembers(ByVal Entry As DirectoryEntry) As String
        Dim groupMembers() As String
        Dim adObject As DirectoryEntry
        Dim vmail As String = "No email"

            For Each member As Object In CType(Entry.Invoke("members", Nothing), System.Collections.IEnumerable)
                adObject = New DirectoryEntry(member)
                If adObject.SchemaClassName.ToString = "group" Then
                              GetGroupMembers(adObject)
                           Else
                    If not adObject.Properties("mail").Value Is Nothing Then vmail = adObject.Properties("mail").Value.ToString
                    DataGridView1.Rows.Add(Label6.Text, adObject.Properties("sAMAccountName").Value.ToString, adObject.Properties("displayname").Value.ToString, vmail)
                    End If
            Next

    End Function

Doh! Function is now a sub...

Sub GetGroupMembers(ByVal Entry As DirectoryEntry)
        Dim adObject As DirectoryEntry
        Dim vmail As String = "No email"

            For Each member As Object In CType(Entry.Invoke("members", Nothing), System.Collections.IEnumerable)
                adObject = New DirectoryEntry(member)
                If adObject.SchemaClassName.ToString = "group" Then
                              GetGroupMembers(adObject)
                           Else
                    If not adObject.Properties("mail").Value Is Nothing Then vmail = adObject.Properties("mail").Value.ToString
                    DataGridView1.Rows.Add(Label6.Text, adObject.Properties("sAMAccountName").Value.ToString, adObject.Properties("displayname").Value.ToString, vmail)
                    End If
            Next

    End Sub
commented: slm +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.