Bet at JAXX

VB Script…

calendar March 19, 2008

To export users from Groups in AD

For work I had to export all the users from various AD groups, this was proving quite a task, but here is a work around if you ever need to do the same!

Just copy the code edit the sections to match your domain and save as a .VBS file

‘ Script to get group membership

strResult = “”
strCount = 0
strFile = “MemberOutput.csv”
set objFSO = createobject(”scripting.filesystemobject”)

if objFSO.fileExists(strFile) then
    set objLog = objFSO.opentextfile(strFile,2)
else               
    objFSO.createtextfile(strFile)
    set objLog = objFSO.opentextfile(strFile,2)
end if

strGroupDN = “CN=[EDIT],OU=[EDIT],OU=[EDIT],DC=[EDIT],DC=[EDIT],DC=[EDIT]“
Set objGroup = GetObject (”LDAP://” & strGroupDN)
objGroup.getInfo

objLog.writeline “”"Membership of: ” & strGroupDN & “”"” & vbcrlf

ProcessGroup strGroupDN
objLog.writeline “Total members: ” & strCount
objLog.writeline “Displayname,Distinguishedname” & vbcrlf
objLog.writeline strResult

sub ProcessGroup(strGroupObject)
    Set objGroup = GetObject (”LDAP://” & strGroupDN)
    arrMemberOf = objGroup.GetEx(”member”)

    For each strGroupObj in arrMemberOf
        set objObject = getObject(”LDAP://” & strGroupObj)
        if objObject.objectcategory=”CN=Person,CN=Schema,CN=Configuration,DC=[EDIT],DC=[EDIT],DC=[EDIT]” then
            strResult = strResult &  “”"” & objObject.displayname & “”"” & “,”"” & objObject.distinguishedname & “”"” & vbcrlf
            strCount = strCount + 1
        else
            ProcessGroup objObject.distinguishedname
        end if
    next

end sub

msgbox “Done”

If you get stuck let me know!

rhodzy

Leave a Reply