Enumerate many Active Directory Groups at once

| | Comments (0) | TrackBacks (0)

Here's another simple script that will simply write out your AD group memberships to a csv file with the name of a group. Input is a simple text file with one group name per line. This script is adapted from the original at WiseSoft.

' VBScript source code
' takes a list of groups in a text file and dumps out a text file with each group's membership.
Set objFSO = CreateObject("Scripting.FileSystemObject")
'change this line to wherever you want to read the input from.
Set objTextFile = objFSO.OpenTextFile("c:\scripts\groups\groups.txt",1)

Do Until objTextFile.AtEndOfStream

groupName = objTextFile.Readline
'Debug.WriteLine groupname
If groupName = "" Then
wscript.quit
End if

groupPath = getgrouppath(groupName)
'Debug.WriteLine groupPath
If groupPath = "" then
wscript.echo "Unable to find the specified group in the domain"
wscript.quit
End if

Set objGroup = getobject(grouppath)
Set objFSO2 = createobject("scripting.filesystemobject")
'change the path to where you want the output files to go.
Set objFile = objFSO2.createtextfile("c:\scripts\groups\" & groupname & ".csv")
q = """"

objFile.WriteLine(q & "sAMAccountName" & q & "," & q & "Surname" & q & "," & q & "FirstName" & q)
For each objMember in objGroup.Members
objFile.WriteLine(q & objmember.samaccountname & q & "," & q & objmember.sn & _
q & "," & q & objmember.givenName & q)
Next

Loop
Set objFile=nothing
'***** Users who's primary group is set to the given group need to be enumerated seperatly.*****
getPrimaryGroupMembers groupName

wscript.echo "Completed"

Function getGroupPath(byval GroupName)
Set cmd=createobject("ADODB.Command")
set cn=createobject("ADODB.Connection")
set rs=createobject("ADODB.Recordset")

cn.open "Provider=ADsDSOObject;"

cmd.commandtext = "SELECT adspath from 'LDAP://" & getnc & _
"' WHERE objectCategory = 'Group' and sAMAccountName = '" & groupname & "'"
cmd.activeconnection = cn

set rs = cmd.execute

if rs.bof <> true and rs.eof<>true then
getgrouppath=rs(0)
else
getgrouppath = ""
end if
cn.close

End function

Function getNC
set objRoot=getobject("LDAP://RootDSE")
getNC=objRoot.get("defaultNamingContext")
End function

Function getPrimaryGroupMembers(byval GroupName)
set cn = createobject("ADODB.Connection")
set cmd = createobject("ADODB.Command")
set rs = createobject("ADODB.Recordset")

cn.open "Provider=ADsDSOObject;"
cmd.activeconnection=cn

'***** Change the Page Size to overcome the 1000 record limitation *****
cmd.properties("page size")=1
cmd.commandtext = "SELECT PrimaryGroupToken FROM 'LDAP://" & getnc & _
"' WHERE sAMAccountName = '" & GroupName & "'"
Set rs = cmd.execute

If rs.eof<>true and rs.bof<>true Then
PrimaryGroupID = rs(0)
Else
Err.Raise 5000, "getPrimaryGroupMembers", "Unable to find PrimaryGroupToken property"
end If

cmd.commandtext = "SELECT samaccountname, sn, givenName, distinguishedName FROM 'LDAP://" & getNC & _
"' WHERE PrimaryGroupID = '" & PrimaryGroupID & "'"

set rs = cmd.execute

while rs.eof<>true and rs.bof<>true
objFile.WriteLine(q & rs("samaccountname") & q & "," & q & rs("sn") & q & _
"," & q & rs("givenName") & q & "," & q & rs("distinguishedName"))
rs.movenext
Wend
cn.close

End Function

0 TrackBacks

Listed below are links to blogs that reference this entry: Enumerate many Active Directory Groups at once.

TrackBack URL for this entry: http://cw.sampas.net/cgi-bin/mt/mt-tb.cgi/175

Leave a comment

About this Entry

This page contains a single entry by Larry published on March 17, 2009 9:56 AM.

How to dump your web hosting provider in one line. was the previous entry in this blog.

Why the telemarketers aren't stopping. is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.