Public Function ListADUsers()
Try
Dim dsUsers As New DataSet
Dim entry As New DirectoryServices.DirectoryEntry("")
Dim mySearcher As New System.DirectoryServices.DirectorySearcher(entry)
Dim result As System.DirectoryServices.SearchResult
dsUsers.Tables.Add()
dsUsers.Tables(0).Columns.Add(New DataColumn("username"))
dsUsers.Tables(0).Columns.Add(New DataColumn("fullname"))
dsUsers.Tables(0).Columns.Add(New DataColumn("title"))
dsUsers.Tables(0).Columns.Add(New DataColumn("mail"))
dsUsers.Tables(0).Columns.Add(New DataColumn("groups"))
'sets the filter for all user accounts
'mySearcher.Filter = "(objectClass=User)"
mySearcher.Filter =
"(&(objectCategory=person)(objectClass=user)(!userAccountControl:1.2.840.113
556.1.4.803:=2))"
'loop through the results and print out the name and displayname
Dim i As Integer = 0
For Each result In mySearcher.FindAll()
'Dim propcoll As ResultPropertyCollection = result.Properties
Dim u As New oUser(result)
Dim fullname As String = u.AccountName
Dim row As DataRow
If InStr(1, fullname, "$", CompareMethod.Binary) Or fullname = "" _
Or InStr(fullname, "{", CompareMethod.Binary) _
Or InStr(1, fullname, "NAV ", CompareMethod.Binary) _
Or InStr(1, fullname, "mail ", CompareMethod.Binary) _
Or InStr(1, LCase(fullname), "account", CompareMethod.Binary) Then
Else
row = dsUsers.Tables(0).NewRow
row("username") = u.AccountName
row("fullname") = u.DisplayName
row("title") = u.Title
row("mail") = u.Email
Dim j As Integer = 0
Dim _Groups As String = u.Groups(u.AccountName)
row("groups") = _Groups
dsUsers.Tables(0).Rows.Add(row)
End If
Next
If dsUsers.Tables(0).Rows.Count < 1 Then
Throw New Exception("No users returned")
End If
Return dsUsers
Catch ex As Exception
Dim sw As New IO.StreamWriter("c:\ws_error.txt")
sw.Write(ex.Message & vbCrLf & vbCrLf & ex.StackTrace)
sw.Close()
sw = Nothing
End Try
End Function