Filtering Printer Status Displays

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

When carrying out routine printer monitoring, you are likely to be concerned only with printers that are experiencing problems. Because of this, you might want to write a script that displays only printers that are not functioning correctly. For example, a print server in a large organization might handle 100 or more printers. If you return the status of every printer, you might have to search through 99 printers that are working in order to identify the single printer that has stopped responding. This not only wastes a considerable amount of time, but also increases the odds that you will overlook the one printer that is not working and thus not take immediate steps to correct the problem.

One of the prime benefits of WMI is that you can fine-tune the information returned. Instead of returning a list of all 100 printers, you can create a query that returns only a list of printers that are not responding. This makes it much faster and easier to identify the printers that are in need of immediate attention.

Scripting Steps

Listing 13.4 contains a script that issues a notification when a printer stops responding. 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.

    To limit data retrieval to printers that have stopped, use a Where clause to restrict the collection to printers with a printer status equal to 1 (Other) or 2 (Unknown).

  4. Use the Count property to check for the number of printers in the collection.
    • If the Count is 0, that means no stopped printers were discovered. In that case, echo a message stating that all printers are functioning correctly.
    • If the Count is greater than 0, then for each printer in the collection echo the name of the printer along with the message that the printer has stopped responding.

Listing 13.4   Receiving Notification When a Printer Stops

1 2 3 4 5 6 7 8 9 10 11 12 13 
strComputer = "." Set objWMIService = GetObject("winmgmts:" _     & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colInstalledPrinters =  objWMIService.ExecQuery _     ("SELECT * FROM Win32_Printer WHERE PrinterStatus = 1" _         & "OR PrinterStatus = 2") If colInstalledPrinters.Count = 0 Then     Wscript.Echo "All printers are functioning correctly." Else     For Each objPrinter in colInstalledPrinters         Wscript.Echo "Printer " & objprinter.Name & " is not responding."     Next End If

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