Monitoring Printer Status by Using a Temporary Event Subscription

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

Monitoring scripts typically check printer status, pause for a predetermined interval (for example, 10 minutes), and then check printer status again. This is done to minimize the processing power and network bandwidth required to monitor printers, and is usually based on an acceptable response time (as determined by your organization) for responding to printer problems. If your users are not severely impacted by a printer that might be out of commission for 10 minutes before support personnel are notified of the problem, then using a standard monitoring script that verifies printer status every 10 minutes is an acceptable approach.

However, for high-volume printers or for printers that print critical business documents (such as invoices), administrators might want immediate notification of any printer problem. For these printers, you can use a temporary event subscription that constantly monitors their status and immediately notifies you the moment one of these printers changes status (for example, a printer that was printing but is now jammed).

Scripting Steps

Listing 13.5 contains a script that monitors printer status by using a temporary event subscription. 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 an ExecNotificationQuery to register for notification each time there is an instance modification (that is, each time an instance within the namespace changes in some way).

    Because the script monitors only changes to printers, include a Where clause that limits data retrieval to instance modifications involving the Win32_Printer class. The additional clause (within 30) causes the script to check every 30 seconds to see whether there has been a change in status. Therefore, status changes will not last longer than 30 seconds before being reported. In theory, a printer could stop and restart in less than 30 seconds. If that happened, the instance modification would go unreported.

  4. Use a loop that allows the script to run indefinitely.

    To stop monitoring, terminate the process the script is running in.

  5. Use the NextEvent method to retrieve the properties of each event when it occurs.

    These properties are retrieved from an event object known as the TargetInstance. Each time a printer is modified in some way, the script checks to see whether the current printer status differs from the previous printer status. If it does not, the script resumes looping. This check is performed by comparing the status property of the TargetInstance with the status property of the PreviousInstance, an event object representing the state of the printer prior to the last instance modification.

    Comparing the previous state with the current state enables you to identify whether the modification involved a change in printer status (for example, going from Idle to Printing). If the previous and current states are the same, the modification did not involve printer status but instead reflects a change to some other property of the printer (for example, a change in the printer location).

    Because printer status is returned as an integer, a pair of Select Case statements is used to convert the current status and the previous status to a more recognizable text string. For example, if the printer status is returned as 3, the word Idle will be displayed as part of the event notification message.

  6. If the printer states differ, echo the printer name, its current state, and its previous state.

Listing 13.5   Monitoring Printer Status Using a Temporary Event Subscription

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 26 27 28 29 30 
strComputer = "." Set objWMIService = GetObject("winmgmts:" _     & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colPrinters = objWMIService. _     ExecNotificationQuery("SELECT * FROM __instancemodificationevent " _         & "WITHIN 30 WHERE TargetInstance ISA 'Win32_Printer'") i = 0 Do While i = 0     Set objPrinter = colPrinters.NextEvent     If objPrinter.TargetInstance.PrinterStatus <> _         objPrinter.PreviousInstance.PrinterStatus Then         Select Case objPrinter.TargetInstance.PrinterStatus             Case 1 strCurrentState = "Other"             Case 2 strCurrentState = "Unknown"             Case 3 strCurrentState = "Idle"             Case 4 strCurrentState = "Printing"             Case 5 strCurrentState = "Warming Up"         End Select         Select Case objPrinter.PreviousInstance.PrinterStatus             Case 1 strPreviousState = "Other"             Case 2 strPreviousState = "Unknown"             Case 3 strPreviousState = "Idle"             Case 4 strPreviousState = "Printing"             Case 5 strPreviousState = "Warming Up"         End Select         Wscript.Echo objPrinter.TargetInstance.Name _             &  " is " & strCurrentState _                 & ". The printer previously was " & strPreviousState & "."     End If 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