Skip to content

Instantly share code, notes, and snippets.

@jkbryan
Last active October 2, 2018 20:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkbryan/41cc18fb3e923c44bc6e8ec4ccea39e0 to your computer and use it in GitHub Desktop.
Save jkbryan/41cc18fb3e923c44bc6e8ec4ccea39e0 to your computer and use it in GitHub Desktop.
get-group-members-from-ad
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