Question Get all users from a security group from AD

sebasjuh

Member
Joined
Dec 6, 2007
Messages
13
Programming Experience
Beginner
Hello,

I'm making a treeview to get all the security groups we have in Active Directory.
This is the code what fills my treeview with that data:

VB.NET:
de = New System.DirectoryServices.DirectoryEntry("LDAP://OU=Apps,DC=domain,DC=com")
srch = New System.DirectoryServices.DirectorySearcher(de)
srch.Filter = "(&(objectClass=group))"

OU = TreeView2.Nodes.Add("Groups", "Groups", 0, 0)

For Each result In srch.FindAll()
   dir = result.GetDirectoryEntry
   users = OU.Nodes.Add(dir.Properties("Name").Value, dir.Properties("Name").Value, 1, 1)
Next

But now is the question I want to get a list of all the users in a specified security group from the AD. That's a hard one and i havn't figured that one out. So maybe someone can help me with this?

I tried it with this code:
Only the problem is I don't get a messagebox with a name. For the test I created a security group named: SecurityGroupName
And added some users to it. But when I execute the script down below I won't get a username...... what am I doing wrong???

Or how can I get a list of all users from a security group in Active Directory?

VB.NET:
de = New System.DirectoryServices.DirectoryEntry("LDAP://CN=SecurityGroupName,OU=Apps,DC=domain,DC=com")
srch = New System.DirectoryServices.DirectorySearcher(de)
srch.Filter = "(&(objectClass=user))"

For Each result In srch.FindAll()
   dir = result.GetDirectoryEntry
   MsgBox(dir.Properties("Name").Value)
Next
 
I have uploaded an example I have previously made in VB2005; showing different functionality with using the active directory.

Look at the button that say's "List users in a group". Click on the button, specify the group name and it will then display a list of all the users in that group.

If you have any problems with it let me know.
 

Attachments

  • ActiveDirectory.zip
    27.5 KB · Views: 464
I have meant to change this to return an array which would then be easier to manipulate after being called. But you can see that is easy to change according to however you might want to use it.

VB.NET:
Public Function GetGroupUsers(ByVal strGroupName As String) As String

        '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        '
        '    Returns all the users of a specific group
        '    Each user on seperate line in the string
        '    The group name being passed is not case sensitive
        '
        '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        Dim dsDirectorySearcher As New DirectorySearcher
        Dim strUsers As String
        Dim intEqualsIndex As Integer
        Dim intCommaIndex As Integer
        Dim sbGroupUsers As New StringBuilder

        'Filter by group name
        With dsDirectorySearcher
            .Filter = "sAMAccountName=" & strGroupName
            .PropertiesToLoad.Add("member")

            Try
                'Retrieve results
                Dim dsResult As SearchResult = .FindOne
                Dim intCounter As Integer

                If dsResult Is Nothing Then
                    'No results returned
                    Return Nothing
                End If

                For intCounter = 0 To dsResult.Properties("member").Count - 1
                    strUsers = dsResult.Properties("member")(intCounter).ToString

                    'Get index of equals and comma
                    intEqualsIndex = strUsers.IndexOf("=", 1)
                    intCommaIndex = strUsers.IndexOf(",", 1)

                    If intEqualsIndex = -1 Then
                        Return Nothing
                    End If

                    'Extract name from string and append to string builder
                    sbGroupUsers.Append(strUsers.Substring((intEqualsIndex + 1), (intCommaIndex - intEqualsIndex) - 1))
                    sbGroupUsers.Append(ControlChars.CrLf)

                Next intCounter

            Catch ex As Exception
                MessageBox.Show("Error in GetGroupUsers Function" & vbNewLine & vbNewLine _
                    & ex.Message, "Active Directory Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

            End Try

        End With

        Return sbGroupUsers.ToString

    End Function
 
I have uploaded an example I have previously made in VB2005; showing different functionality with using the active directory.

Look at the button that say's "List users in a group". Click on the button, specify the group name and it will then display a list of all the users in that group.

If you have any problems with it let me know.

Hi, I know this is an very old post, but maybe you're still around. I'm using your code and is very usefull, how should I modify de "GetGroupUsers" sub to get the logins beside the full names?

Thanks!
 
Back
Top