Microsoft® Windows® 2000 Scripting Guide
« Previous | Next »
Publishing printers in Active Directory is a convenience for users: They can easily locate printers, especially if printer location tracking is enabled. However, publishing printers in Active Directory can also be a convenience for administrators: Administrators can retrieve a list of shared printers simply by searching for these printers in Active Directory, by using GUI tools or by using a script that searches Active Directory.
To enumerate all the published printers, you can search Active Directory by using the Active Directory OLE DB provider.
Scripting Steps
Listing 13.25 contains a script that enumerates all the printers published in Active Directory. To carry out this task, the script must perform the following steps:
- Insert an On Error Resume Next Statement. This prevents the script from failing if no printers can be found in Active Directory.
- Create a constant named ADS_SCOPE_SUBTREE and set the value to 2. This will be used to specify a search that begins in the Active Directory root and proceeds to search all the child containers as well.
- Create an instance of the Active Directory connection object (ADODB.Connection).
This allows you to connect to Active Directory.
- Set the provider property of the connection object to the Active Directory provider (ADsDSOObject). This is the OLE database provider for ADSI.
- Set the active connection to the Active Directory connection.
- Set the command text for the Active Directory command object to the SQL query that retrieves all the printers from fabrikam.com.
To ensure a quicker search, include only the attributes printerName and serverName in the SQL query.
- Specify values for page size, time-out, search scope, and caching.
- Execute the SQL query.
- When the set of printers is returned, use the MoveFirst method to move to the first printer in the recordset.
- For each printer in the recordset, echo the printer name and the print server name.
Listing 13.25 Enumerating All the Published Printers in Active Directory
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| On Error Resume Next 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 printerName, serverName FROM " _ & " 'LDAP://DC=fabrikam,DC=com' WHERE objectClass='printQueue'" 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 "Printer Name: " & objRecordSet.Fields("printerName").Value Wscript.Echo "Server Name: " & objRecordSet.Fields("serverName").Value objRecordSet.MoveNext Loop |
Send us your feedback | « Previous | Next » |