Enumerating Printer Connections

Microsoft® Windows® 2000 Scripting Guide

Enumerating the printer connections on client computers can help you better understand why print resources are (or are not) being used. For example, there could be a number of reasons why Printer A attracts twice the workload of Printer B:

  • Printer A might be faster than Printer B.

  • Printer A might be more reliable than Printer B.

  • Printer A might be more conveniently located than Printer B.

On the other hand, Printer A might receive a disproportionate amount of the workload simply because twice as many users have configured Printer A to be their default printer. In a case such as this, you can facilitate the printing process by changing the default printer for some of your users. Before you can do this, however, you must know which printers have been connected to which computers. This information can be obtained by using the Win32_Printer class.

note Note

  • The WSH method EnumPrinterConnections can also be used to enumerate the printer connections on a computer. However, any script using EnumPrinterConnections must be run on the local computer. By contrast, WMI can be used to enumerate printer connections on remote computers.

Scripting Steps

Listing 13.22 contains a script that enumerates printer connections. To carry out this task, the script must perform the following steps:

  1. Create a variable to specify the computer name.

  2. Use a GetObject call to connect to the WMI namespace root\cimv2, and set the impersonation level to "impersonate."

  3. Use the ExecQuery method to query the Win32_Printer class.

    This query returns a collection consisting of all the printers installed on the computer.

  4. For each printer in the collection, echo the printer name and location, and whether the printer is the default printer.

Listing 13.22 Enumerating Printer Connections


strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_Printer")
For Each objPrinter in colInstalledPrinters
 Wscript.Echo "Name: " & objPrinter.Name
 Wscript.Echo "Location: " & objPrinter.Location