Monitoring Printer Status

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

The best way to minimize the impact of a printer that is no longer working or accessible is to identify the problem and restore the printer functionality as quickly as possible. Even if this functionality cannot be restored (for example, the printer might have experienced a hardware problem that you are unable to repair), it is still important that printer problems be identified as quickly as possible; at the very least, this allows you to notify users that they should reroute their documents and not print anything to the "broken" printer.

You can use the Win32_Printer class to retrieve a status report for all the printers managed by a given print server.

Scripting Steps

Listing 13.2 contains a script that monitors the status of all the printers on a computer. 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 values such as printer share name, printer location, and the current printer status.

    Because printer status is returned as an integer, a series of Select Case statements is used to convert the integer to a recognizable text string. For example, if PrinterStatus is 4, the value "Printing" is echoed to the screen.

Listing 13.2   Monitoring the Status of All the Printers on a Computer

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 
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     Select Case objPrinter.PrinterStatus         Case 1             strPrinterStatus = "Other"         Case 2             strPrinterStatus = "Unknown"         Case 3             strPrinterStatus = "Idle"         Case 4             strPrinterStatus = "Printing"         Case 5             strPrinterStatus = "Warmup"     End Select     Wscript.Echo  "Printer Status: " & strPrinterStatus     Wscript.Echo  "Server Name: " & objPrinter.ServerName     Wscript.Echo  "Share Name: " & objPrinter.ShareName     Wscript.Echo Next

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