Created
November 19, 2013 17:28
-
-
Save milsosa/7549069 to your computer and use it in GitHub Desktop.
Get list of Active Directory Users
This file contains hidden or 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
' Get OU | |
' | |
strOU = "OU=Users,DC=domain,DC=com" | |
' Create connection to AD | |
' | |
Set objConnection = CreateObject("ADODB.Connection") | |
objConnection.Open "Provider=ADsDSOObject;" | |
' Create command | |
' | |
Set objCommand = CreateObject("ADODB.Command") | |
objCommand.ActiveConnection = objConnection | |
objCommand.Properties("Page Size") = 1000 | |
' Execute command to get all users in OU | |
' | |
objCommand.CommandText = _ | |
"<LDAP://" & strOU & ">;" & _ | |
"(&(objectclass=user)(objectcategory=person));" & _ | |
"adspath,distinguishedname,sAMAccountName;subtree" | |
Set objRecordSet = objCommand.Execute | |
' Show info for each user in OU | |
' | |
Do Until objRecordSet.EOF | |
' Show required info for a user | |
' | |
WScript.Echo objRecordSet.Fields("adspath").Value | |
WScript.Echo objRecordSet.Fields("distinguishedname").Value | |
WScript.Echo objRecordSet.Fields("sAMAccountName").Value | |
' Move to the next user | |
' | |
objRecordSet.MoveNext | |
Loop | |
' Clean up | |
' | |
objRecordSet.Close | |
Set objRecordSet = Nothing | |
Set objCommand = Nothing | |
objConnection.Close | |
Set objConnection = Nothing |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment