Wednesday, September 15, 2010

VBScript to enumerate domain computers

Ever need to list all the computers in your Active Directory? This handy script will query AD and return all computer objects. A little tweaking and you can search for objects based on last modified time to ensure you are listing valid AD objects.



'EnumComputers.vbs

Const ADS_SCOPE_SUBTREE = 2
strDomain = "mydomain.com"

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"

Set objCOmmand.ActiveConnection = objConnection
objCommand.CommandText = "Select Name, Location from 'LDAP://" & strDomain & "' Where objectCategory='computer'"  
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst

Do Until objRecordSet.EOF
    wscript.echo objRecordSet.Fields("Name").Value
'    Wscript.Echo "Computer Name: " & objRecordSet.Fields("Name").Value
'    Wscript.Echo "Location: " & objRecordSet.Fields("Location").Value
    objRecordSet.MoveNext
Loop

No comments: