Enumerating All the Published Printers in a Domain

Microsoft® Windows® 2000 Scripting Guide

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:

  1. Insert an On Error Resume Next Statement. This prevents the script from failing if no printers can be found in Active Directory.
  2. 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.
  3. Create an instance of the Active Directory connection object (ADODB.Connection).

    This allows you to connect to Active Directory.

  4. Set the provider property of the connection object to the Active Directory provider (ADsDSOObject). This is the OLE database provider for ADSI.
  5. Set the active connection to the Active Directory connection.
  6. 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.

  7. Specify values for page size, time-out, search scope, and caching.
  8. Execute the SQL query.
  9. When the set of printers is returned, use the MoveFirst method to move to the first printer in the recordset.
  10. 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 Send us your feedback « Previous | Next »   


Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
Microsoft Windows 2000 Scripting Guide(c) Automating System Administration 2003
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 635

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net