I struggled with the same issue a while ago. Here is a class that I pieced together from info gathered throughout the internet. The output is a dataset of employee names. This has worked pretty good for my needs, hope it helps.
Public Class clsActiveDirectory
Private Function CreateEmptyDataset()
Dim ds As New DataSet
Dim dt As DataTable
dt = ds.Tables.Add("tblEmployees")
'add column
dt.Columns.Add("GivenName", GetType(String))
dt.Columns.Add("SN", GetType(String))
dt.Columns.Add("DisplayName", GetType(String))
Return ds
End Function
Private Function GetLDAPPaths() As DataSet
'This function retrieves LDAP Paths from a database.
'It is a one field table. The field name is LDAPpath
'Here is an example of an entry.
'OU=Inventory Control,OU=mylocation, DC=myserver,DC=local
Dim objConn As New myDatabaseConnectionClass
Dim conn As New SqlClient.SqlConnection
Dim strSelect As String = "SELECT * "
Dim strFrom As String = "FROM myLDAPPathsTable"
Dim strSQL As String = strSelect & strFrom
conn = objConn.connBackEndDB
Dim cmd As New SqlClient.SqlCommand(strSQL, conn)
Dim da As New SqlClient.SqlDataAdapter(cmd)
Dim ds As New DataSet
da.Fill(ds, "tblAD")
Return ds
End Function
Public Function GetMembers() As DataSet
Dim dsLDAP As DataSet = GetLDAPPaths()
Dim o(2) As Object
Dim ds As DataSet = CreateEmptyDataset()
Dim entry As New System.DirectoryServices.DirectoryEntry
Dim mySearcher As New System.DirectoryServices.DirectorySearcher
Dim results As System.DirectoryServices.SearchResultCollection
Dim resEnt1 As System.DirectoryServices.SearchResult
Dim r As DataRow
Dim strPath As String
Dim strGivenName As String
Dim strSN As String
Dim strDisplayName As String
Dim oProp As Object
For Each r In dsLDAP.Tables(0).Rows
strPath = r.Item("LDAPpath").ToString
entry.Path = ("LDAP://" & strPath)
'create a directory searcher which we wrap around the
'directory object we want to search
mySearcher.SearchRoot = entry
mySearcher.Filter = String.Format("(cn=*)")
mySearcher.PropertiesToLoad.Add("givenName")
mySearcher.PropertiesToLoad.Add("sn")
mySearcher.PropertiesToLoad.Add("displayName")
'perform the search and get the result collection back
results = mySearcher.FindAll
'now loop through all the objects in the result collection
'get the found directory entry
For Each resEnt1 In results
If resEnt1.Properties.Contains("givenName") Then
For Each oProp In resEnt1.Properties("givenName")
strGivenName = oProp.ToString
o(0) = strGivenName
Next
Else
o(0) = ""
End If
If resEnt1.Properties.Contains("sn") Then
For Each oProp In resEnt1.Properties("sn")
strSN = oProp.ToString
o(1) = strSN
Next
Else
o(1) = ""
End If
If resEnt1.Properties.Contains("displayName") Then
For Each oProp In resEnt1.Properties("displayName")
strDisplayName = oProp.ToString
o(2) = strDisplayName
Next
Else
o(2) = ""
End If
ds.Tables(0).Rows.Add(o)
Next
Next
results.Dispose()
mySearcher.Dispose()
entry.Close()
entry.Dispose()
Return ds
End Function
End Class