Searching for Specific Printers in a Domain
Microsoft® Windows® 2000 Scripting Guide
In addition to enumerating all the printers in a domain, you can search for specific printers. To do this, you simply search for printers that have one or more of the attributes of the printQueue object in Active Directory.
For example, to find all the color printers in the domain, you would search for all printers for which the printsColor attribute is True. To find printers that support stapling, you would search for all printers for which the printStaplingSupported attribute is True.
The printQueue object attributes are shown in Table 13.9.
Table 13.9 Attributes for the printQueue Object
Attribute |
Friendly Name |
Description |
---|---|---|
cn |
Directory Service Name |
Name of the printer as viewed in Active Directory Users and Computers. |
uNCName |
Network Name |
UNC path of the printer. This attribute is populated by the print spooler when the printQueue object is created. |
assetNumber |
Asset Number |
Asset number assigned to the printer. This attribute can be configured by using ADSI. |
contactName |
Contact |
Name of the person to contact regarding the printer. This attribute can be configured by using ADSI. |
description |
Comment |
Description of the printer. This is derived from the printer object at the time the printQueue object is created. |
driverName |
Model |
Model of the printer. This is derived from the printer driver at the time the printQueue object is created. |
location |
Location |
Physical location of the printer. This attribute can be configured by using ADSI. |
portName |
Port |
Name of the port(s) supporting this printer. This attribute is populated by the print spooler when the printQueue object is created. |
printBinNames |
Input Trays |
Input tray names. This attribute is populated by the printer driver when the printQueue object is created. |
printCollate |
Supports Collation |
Indicates whether or not the printer supports collation. This attribute is populated by the printer driver when the printQueue object is created. |
printColor |
Supports Color Printing |
Indicates whether or not this is a color device. This attribute is populated by the printer driver when the printQueue object is created. |
printDuplexSupported |
Supports Double-sided Printing |
Indicates whether or not this device can print on both sides of the paper. This attribute is populated by the printer driver when the printQueue object is created. |
printerName |
Name |
Name of the printer. This attribute is populated by the print object when the printQueue object is created. |
printLanguage |
Printer Language |
Page description language used by the printer. This attribute is populated by the printer driver when the printQueue object is created. |
printMaxResolutionSupported |
Maximum Resolution |
Maximum resolution (in DPI). This attribute is populated by the printer driver when the printQueue object is created. |
printMediaReady |
Paper Available |
Paper types loaded in the device at the present time. This attribute is populated by the printer driver when the printQueue object is created. |
printMediaSupported |
Paper Types Supported |
Media types that the printer supports. This attribute is populated by the printer driver when the printQueue object is created. |
printMemory |
Installed Memory |
Amount of installed memory. This attribute is populated by the printer driver when the printQueue object is created. |
printOwner |
Owner Name |
Name of the person or group that owns the printer. This attribute can be configured by using ADSI. |
printRate |
Speed |
Speed of the device. This attribute is populated by the printer driver when the printQueue object is created. |
printRateUnit |
Speed Units |
Units that the printer speed is measured in. This attribute is populated by the printer driver when the printQueue object is created. |
printPagesPerMinute |
Pages per Minute |
Printer speed normalized to pages per minute. This attribute is populated by the printer driver when the printQueue object is created. |
printShareName |
Share Name |
Share name of the printer. This attribute is populated by the print object when the printQueue object is created. |
printStaplingSupported |
Supports Stapling |
Indicates whether or not the device can staple. This attribute is populated by the printer driver when the printQueue object is created. |
serverName |
Server Name |
Name of the server supporting the printer. This attribute is populated by the print spooler when the printQueue object is created. |
url |
Web Page Address |
URL of the printer's Web page. This attribute is populated by the print spooler when the printQueue object is created. |
versionNumber |
Object Version |
Internal version number of the printer object. This attribute is populated by the print spooler when the printQueue object is created. |
Scripting Steps
Listing 13.26 contains a script that searches for specific printers 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).
The connection object allows you to connect to Active Directory.
Create an instance of the Active Directory command object (ADODB.Command).
The command object allows you to issue queries and other database commands using the Active Directory connection.
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 printer data from fabrikam.com.
To limit data retrieval to printers with a priority of 2, add the statement "and Priority = 2" to the Where clause. 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.
Run 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.26 Searching for Specific Printers in Active Directory
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|