OK, this will find all of the members of a specific group, you just need to change the lines that I have added a comment above to suit your AD. Getting the phone number attribute of each user shouldnt be difficult now that you have the full distinguished name (path) of each user account.
vb Code:
Dim GroupSearcher As New DirectorySearcher
'<<<<< Change the line below to your domain and your domain admin details >>>>>>
Dim GroupSearchRoot As New DirectoryEntry("LDAP://CN=Users,DC=yourdomainname,DC=local", "Your_Domain_Admin", "Admin_Password")
With GroupSearcher
.SearchRoot = GroupSearchRoot
.Filter = "(&(ObjectClass=Group)(CN=_All Employees))"
End With
Dim Members As Object = GroupSearcher.FindOne.GetDirectoryEntry.Invoke("Members", Nothing)
For Each Member As Object In CType(Members, IEnumerable)
Dim CurrentMember As New DirectoryEntry(Member)
ListBox1.Items.Add(CurrentMember.Name.Remove(0, 3))
Next
Notice that I'm just adding the list of users to a listbox named Listbox1 so if you wanted to try my code out you would need to add a listbox with the same name to your form.
OK so as you said you were trying to understand how this works, I'll give you a bit of an explanation :)
This first 2 lines of the code are just declaring the search object and the root of the search and passing in the username/password.
In the next part of the code, I am just setting a couple of properties of the searcher object. Namely, the SearchRoot property so that it knows where to look in AD (this saves it from searching all of the containers and OUs). The other property, Filter, is used to construct an LDAP filter that tells the searcher we only want to look for objects that are Groups and that we only want to look for objects that have a CN (Common Name) that matches our group name. Pretty simple stuff yeah?
The next line:
Code:
Dim Members As Object = GroupSearcher.FindOne.GetDirectoryEntry.Invoke("Members", Nothing)
is what actually gets the list of members from the object that was returned by the search. We could make this look a bit simpler by doing something like this instead but it still acheives the exact same result:
vb Code:
Dim Result As SearchResult = GroupSearcher.FindOne
Dim Members = Result.GetDirectoryEntry.Invoke("Members", Nothing)
Then all we do is loop through the collection of members and create a new DirectoryEntry object for each one, which then allows us to use the DirectoryEntry's
Name property to retrieve the user's CN (common name).
Oh and in case your wondering - The Name property always has "CN=" prefixed to it so thats why I use
Name.Remove(0,3) instead of just
Name when adding it to the listbox.
So! I've done the hard work for you, now if you want to get all of the users telephone numbers then all you need to do is use the collection of CNs that you retrieve from my code. Instead of adding the Name property to a listbox, you would use the Properties property (confusing huh) to find specific attributes on the AD account. I'll give you a tip, the phone number attribute's exact name is just "telephoneNumber" and the mobile phone number attribute is just "mobile".
That help? :)
Chris