Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding Usernames in a specific Group using Active Directory

Status
Not open for further replies.

markhkram

IS-IT--Management
Dec 30, 2008
32
US
Hi, I have this code that will display a list of users in a specific group in an Active Directory Domain. It works great, except it is giving the users full name (John Doe) in the MsgBox instead of login account name (jdoe). Anyone know how I can adjust this to show the login account name (jdoe) instead? Here is what I have so far:

'vb2005
Dim dsDirectoryEntry As New DirectoryEntry("LDAP://" & strDomain)
Dim dsDirectorySearcher As New DirectorySearcher(dsDirectoryEntry)
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

MsgBox(strUsers.Substring((intEqualsIndex + 1), (intCommaIndex - intEqualsIndex) - 1))

Next intCounter

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

End Try

End With

 
These are just guesses because I've not tried to pull the account name and can't check right now. Try replacing member with anr or samaccountname. Anr is what I use to filter for user name, but for some reason I don't have it in my list of available properties. I don't know if I left it out by mistake or what.


-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
I tried replacing member with anr and samaccountname, but both didn't work, didn't produce anything.
 
Here. Setup a textbox or richtextbox to receive the output and slap this in a button to find all the members/values avaible.

Code:
Dim entry As New DirectoryServices.DirectoryEntry()
        Dim mySearcher As New System.DirectoryServices.DirectorySearcher(entry)
        Dim result As System.DirectoryServices.SearchResult
        Dim objCollResultProperty As System.DirectoryServices.ResultPropertyCollection

        RichTextBox1.Clear()

        mySearcher.Filter = "sAMAccountName=" & strGroupName

        For Each result In mySearcher.FindAll
            objCollResultProperty = result.Properties
            For Each obj As Object In objCollResultProperty
                For Each obj2 As Object In obj.Value
                    RichTextBox1.Text &= obj.key & " -- " & obj2.ToString & vbCrLf
                Next
            Next
        Next

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top