Active Directory: VBscript to enumerate all computers in your company

Posted February 26th, 2010 in computer by dirk adamsky

Today’s script is about active directory computer accounts (servers and workstations).
The script is a simple ADO query.
when needed you can add extra computer object attributes.

Follow the next steps to run the script (no admin rights needed):

* open your favorite text editor
* copy and paste the script into the editor
* save the script (for example c:\temp\computers.vbs)
* open a command prompt
* go to “c:\temp”
* give “cscript computers.vbs” (without quotes) and enter

The script:

' Name : computers.vbs
' Description : script to enumerate all computers in your company/AD domain
' Author : dirk adamsky - deludi bv
' Version : 1.00
' Date : 26-02-2010
' Level : intermediate

' Setup ADO objects.
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection

' Search entire Active Directory domain.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
strBase = "<LDAP://" & strDNSDomain & ">"

' Filter on computer objects.
strFilter = "(&(objectCategory=computer))"

' Comma delimited list of attribute values to retrieve.
strAttributes = "name"

' Construct the LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False

' Run the query.
Set adoRecordset = adoCommand.Execute

' Enumerate the resulting recordset.
Do Until adoRecordset.EOF
	On Error Resume Next
	Wscript.echo adoRecordset.Fields("name").Value
	'Move to the next record in the recordset.
    adoRecordset.MoveNext
Loop
' Clean up.
adoRecordset.Close
adoConnection.Close

Set adoRecordset = Nothing
Set objRootDSE = Nothing
Set adoConnection = Nothing
Set adoCommand = Nothing

When you have problems/questions please post a reply.
If you like the script you can rate it.

Happy scripting.

Best regards,

Dirk Adamsky – Deludi BV