Directory Services

Getting Started with Scripting for ADSI

Scripting technology is ideal for system administrators that must create batch scripts for frequently used tasks.

To start scripting with ADSI, you must either have a computer that runs on Windows 2000 or the Windows Server 2003 family operating systems or be logged on to a domain that contains data for computer accounts in the directory.

A Simple Scripting Sample: Finding Names and Locations of Computer Accounts

Create a new text file using a text editor. The following code example shows how to find names and locations of computer accounts.

'---------------------------------------------------------------
' Returns the name and location for all the computer accounts in 
' Active Directory.
'--------------------------------------------------------------- 
Const ADS_SCOPE_SUBTREE = 2
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://DC=fabrikam,DC=com' " _
		& "where objectClass='computer'"  
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Timeout") = 30 
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
objCommand.Properties("Cache Results") = False 
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Do Until objRecordSet.EOF
	Wscript.Echo "Computer Name: " & objRecordSet.Fields("Name").Value
	Wscript.Echo "Location: " & objRecordSet.Fields("Location").Value
	objRecordSet.MoveNext
Loop

Save the file as First.vbs. At the command prompt, type cscript First.vbs for a command line or First.vbs for Windows scripting. The results should be returned in the command prompt.

For more information about scripting for ADSI, see the Active Directory Service Interfaces Scripting Tutorial.