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
'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