In this section, you will practice creating an ADO query to Active Directory to pull out information about computer objects.
1. | Open \My Documents\Microsoft Press\VBScriptSBS\Templates\BlankTemplate.vbs in Notepad or your favorite script editor and save your new script as YourNameStepBy StepADOquery.vbs.
|
2. | Type Option Explicit on the first line to force the declaration of all variables.
|
3. | Type On Error Resume Next.
|
4. | Declare the following variables by using the Dim command: qQuery, objConnection, objCommand, and objRecordSet.
|
5. | Create a query using the LDAP namespace that connects to your local domain controller. Specify that objectCatagory is equal to computer. Choose the following fields: distinguishedName, name, and logonCount. Set the search dimension to subtree. Assign this query to a variable called qQuery. Your code will look similar to the following (make sure you specify the actual name of your domain):
qQuery = "<LDAP://dc=nwtraders,dc=msft" & _
"(objectCategory=computer)" & _
";distinguishedName,name" & _
",operatingSystem" & _
",logonCount" & _
";subtree"
|
6. | Create a variable called objConnection and use it to hold an instance of the ADODB connectionObject. Your code will look like the following:
Set objConnection = CreateObject("ADODB.Connection")
|
7. | Create an ADODB command object and assign it to a variable called objCommand. Your code will look like the following:
Set objCommand = CreateObject("ADODB.Command")
|
8. | Open the connection using connectionObject and specify the ADsDSOObject provider. Your code will look like the following:
objConnection.Open "Provider=ADsDSOObject;"
|
9. | Use the ActiveConnection method of the objCommand object to specify the connection held by objConnection as the active connection to Active Directory. Your code will look like the following:
objCommand.ActiveConnection = objConnection
|
10. | Use the commandText method to set the query contained in the variable qQuery to be the command text for the command object. Your code will look like the following:
objCommand.CommandText = qQuery
|
11. | Assign the record set returned by the Execute method of commandObject to the variable objRecordSet. Your code will look like the following:
Set objRecordSet = objCommand.Execute
|
12. | Use a While Not Wend construction to iterate through the record set and echo out the following fields: Name, distinguishedName, operatingSystem, and logonCount.
|
13. | Once you echo out these fields, use the moveNext method of the objectRecordSet object to advance to the next record. Your code will look like the following:
While Not objRecordSet.EOF
WScript.Echo objRecordSet.Fields("name")
WScript.Echo objRecordSet.Fields("distinguishedName")
WScript.Echo objRecordSet.Fields("operatingSystem")
WScript.Echo objRecordSet.Fields("logonCount")
objRecordSet.MoveNext
Wend
|
14. | Close the connection. Your code will look like the following:
|
15. | Save and run the script by using CScript. If there are problems with your script, then compare it to \My Documents\Microsoft Press\VBScriptSBS\ch13\StepByStep\StepByStepADOQuery.vbs.
|