Wednesday, July 13, 2011

Output list of users in AD group

Totally unrelated to virtualization, but today I needed to get a text list of all users in a group in AD so I copied the following script from the website. I named my script ListUsersInGroup.vbs;

Set objGroup = GetObject(LDAP://CN=XXX,OU=Groups,DC=Company,DC=com)
For Each strUser in objGroup.Member
    Set objUser =  GetObject("LDAP://" & strUser)
    ReDim Preserve arrNames(intSize)
    arrNames(intSize) = objUser.CN
    intSize = intSize + 1


For i = (UBound(arrNames) - 1) to 0 Step -1
    For j= 0 to i
        If UCase(arrNames(j)) > UCase(arrNames(j+1)) Then
            strHolder = arrNames(j+1)
            arrNames(j+1) = arrNames(j)
            arrNames(j) = strHolder
        End If


For Each strName in arrNames
    Wscript.Echo strName

To get the CN identity of the group to I ran the following from the command line;

dsquery group -name AD_GroupName > group.txt

Then I opened up the group.txt file and copied this data into my script, replacing everything after LDAP:// with the CN name of the group.

To collect the output of the VBS script into a text file I ran the following from the command line;

cscript c:\scripts\ListUsersInGroup.vbs >> c:\scripts\Users.txt

No comments:

Post a Comment