>OK, I've given in...
>This must seem mundane to you guys, or maybe I'm just
having a bad hair day,
>but all I want to do is...
>Return an ordered list (on Name) with accompanying
telephone number of all
>of the users in a particular group with ADSI.
>I have scoured the internet, tried various combinations
of methods to no
>avail, I must be missing something simple.
>Can anyone enlighten me.
I would not call this task easy. If I understand the
question, it's a bit of a challenge. I assume you want to
sort the list by sAMAccountName, which is the NT name,
also called the pre-W2k logon name.
I see two approaches. The first would be:
1. Bind to the group object with the LDAP provider (WinNT
does not expose TelephoneNumber).
2. Enumerate the group Members collection.
3. For each group member, populate an array with the
sAMAccountName and TelephoneNumber properties. The array
would be dynamic, and you would ReDim it for each user.
4. Sort the array by sAMAccountName, using a bubble sort
5. Loop thru the array to create your list.
The method I chose:
1. Using ADO, search AD for all user objects where one
item in the MemberOf collection attribute is equal to the
AdsPath of the group. Request the attributes
sAMAccountName and TelephoneNumber, and order the
recordset by sAMAccountName.
2. Loop thru the recordset and create the list.
I hard coded the group AdsPath below. More code would be
needed if you started with just the sAMAccountName of the
group (you would have to use the NameTranslate object to
convert to the LDAP AdsPath). Also, I assumed that all
objects found are user objects.
sGroup = "cn=MyGroup,cn=Users,dc=MyDomain,dc=com"
Set oRoot = GetObject("LDAP://RootDSE")
sDNSDomain = oRoot.Get("DefaultNamingContext")
Set oCommand = CreateObject("ADODB.Command")
Set oConnection = CreateObject("ADODB.Connection")
oConnection.Provider = "ADsDSOObject"
oConnection.Open = "Active Directory Provider"
oCommand.ActiveConnection = oConnection
sQuery = _
"SELECT sAMAccountName, TelephoneNumber FROM 'LDAP://" _
& sDNSDomain & "' WHERE MemberOf = '" _
& sGroup & "' ORDER by sAMAccountName"
oCommand.CommandText = sQuery
oCommand.Properties("Page Size") = 100
Set oResults = oCommand.Execute
If oResults.EOF Then
MsgBox "No users found"
Do Until oResults.EOF
sName = oResults.Fields("sAMAccountName")
sPhone = oResults.Fields("TelephoneNumber")
sDisplay = sName & ", " & sPhone & vbCrLf
I don't know the format you want for the list.