Search AD

Search Active Directory for either Users, Computers or Groups.
The LDAP query string below can be modified to return different information. Alternative search query strings can be written using the Active Directory Users and Computers (ADUC) GUI.

'SearchAD.vbs
On Error Resume Next
' Connect to the LDAP server's root object
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
strTarget = "LDAP://" & strDNSDomain
wscript.Echo "Starting search from " & strTarget

' Connect to Ad Provider
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"

Set objCmd =   CreateObject("ADODB.Command")
Set objCmd.ActiveConnection = objConnection 

' Show only computers
objCmd.CommandText = "SELECT Name, ADsPath FROM '" & strTarget & "' WHERE objectCategory = 'computer'"

' or show only users
'objCmd.CommandText = "SELECT Name, ADsPath FROM '" & strTarget & "' WHERE objectCategory = 'user'"

' or show only groups
'objCmd.CommandText = "SELECT Name, ADsPath FROM '" & strTarget & "' WHERE objectCategory = 'group'"
Const ADS_SCOPE_SUBTREE = 2 objCmd.Properties("Page Size") = 100 objCmd.Properties("Timeout") = 30 objCmd.Properties("Searchscope") = ADS_SCOPE_SUBTREE objCmd.Properties("Cache Results") = False Set objRecordSet = objCmd.Execute ' Iterate through the results objRecordSet.MoveFirst Do Until objRecordSet.EOF sComputerName = objRecordSet.Fields("Name") wscript.Echo sComputerName objRecordSet.MoveNext Loop

Examples

cscript SearchAD.vbs

cscript SearchAD.vbs >output.txt

“Face the facts of being what you are, for that is what changes what you are” ~ Soren Kierkegaard

Related:

ComputerInfo - List properties of a Computer (as shown in ADUC)
CMD: DSQUERY - Search for an active directory object.
Powershell : Get-adComputer - Get-adUser - Get-adGroup


© Copyright SS64.com 1999-2014
Some rights reserved