Last active
October 2, 2018 20:06
get-group-members-from-ad
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Const ADS_SCOPE_SUBTREE = 2 | |
Const ForWriting = 2 | |
' | |
strLDAP="OU=GroupOU,DC=contoso,DC=com" | |
' | |
Set objConnection = CreateObject("ADODB.Connection") | |
Set objCommand = CreateObject("ADODB.Command") | |
objConnection.Provider = "ADsDSOObject" | |
objConnection.Open "Active Directory Provider" | |
Set objCommand.ActiveConnection = objConnection | |
objCommand.Properties("Page Size") = 1000 | |
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE | |
objCommand.CommandText = _ | |
"SELECT distinguishedName FROM 'LDAP://" & strLDAP & "' WHERE objectCategory='group'" | |
Set objRecordSet = objCommand.Execute | |
objRecordSet.MoveFirst | |
Do Until objRecordSet.EOF | |
strGroupName = objRecordSet.Fields("distinguishedName").Value | |
GetMember(strGroupName) | |
objRecordSet.MoveNext | |
Loop | |
Function GetMember(strGroupName) | |
Set objGroup = GetObject("LDAP://" & strGroupName) | |
Wscript.Echo "Group Name: " & objGroup.displayName | |
strPath = "C:\PathToOutputFile\" | |
Set objFso = CreateObject("Scripting.FileSystemObject") | |
'Output filename is the displayname of the group | |
Set objOutputFile = objFSO.OpenTextFile(strPath & objGroup.displayName & ".txt", 2, True) | |
For Each objMember in objGroup.Members | |
'Entry written to file is the samAccountName | |
strCN=objMember.cn | |
objOutputFile.writeline strCN | |
Next | |
objOutputFile.Close | |
End Function |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment