Previous Page
Next Page

Creating a Filtered Print Monitor

One cool thing you can do is filter out only the information you need prior to presenting it to the screen. A Windows Server 2003 print server commonly hosts a couple of hundred printers, so searching through all the print devices looking for one that is offline could take a long time. By making just a couple of changes to the MonitorPrinterStatus.vbs script, you can allow Microsoft Visual Basic, Scripting Edition (VBScript) to perform the weeding work for you.

Just the Steps

To use a filter on the Win32_Printer class to manage a printer

1.
Declare a variable to hold a connection into WMI.

2.
Use GetObject and the WMI moniker to make a connection into WMI.

3.
Assign the object that comes back from the WMI connection to the variable declared in step 1.

4.
Use the ExecQuery method with a Where clause to query Win32_Printer. The Where clause should look for 1, 2, or 7 in the PrinterStatus property.

5.
Use the Count property to determine the population of the collection of printers. If the collection of printers is empty, echo a message to that effect.

6.
If the collection of printers is not empty, use For Each...Next to iterate through the collection.


The revised printer monitor script is called FilterPrinterStatus.vbs. Only a couple of changes were made to affect filtering. The addition of the Where clause to the WMI query takes place in the Reference information section. The use of If...Then...Else in conjunction with the Count property takes place in the Output information section. The FilterPrinterStatus.vbs script follows:

FilterPrinterStatus.vbs

Option Explicit
'On Error Resume Next
Dim strComputer
Dim wmiNS
Dim wmiQuery
Dim objWMIService
Dim colItems
Dim objItem

strComputer = "."
wmiNS = "\root\cimv2"
wmiQuery = "Select * from win32_Printer" _
   & " Where PrinterStatus = 1" _
   & " or PrinterStatus = 2" _
   & " or PrinterStatus = 7"
Set objWMIService = GetObject("winmgmts:\\" _
   & strComputer & wmiNS)
Set colItems = objWMIService.ExecQuery(wmiQuery)

If colItems.count = 0 Then
   WScript.Echo "All printers are fine"
Else
For Each objItem in colItems
  WScript.Echo "Name: " & objItem.Name
  WScript.Echo "Location: " & objItem.Location
  WScript.Echo "Printer Status: " & funEvalStatus(objItem.printerStatus)
  WScript.Echo "Server Name: " & objItem.ServerName
  WScript.Echo "Share Name: " & objItem.ShareName
  WScript.Echo
Next
End If

Function funEvalStatus(intIN)
Select Case intIN
    Case 1
      funEvalStatus = "Other"
    Case 2
      funEvalStatus = "Unknown"
    Case 3
      funEvalStatus = "Idle"
    Case 4
      funEvalStatus = "Printing"
    Case 5
      funEvalStatus = "Warmup"
    Case 6
    funEvalStatus = "Stopped Printing"
    Case 7
    funEvalStatus = "Offline"
  End Select
End Function

Reference Information

The Reference information section is where you modify your WMI query. The only change is adding a compound Where clause to the value you assigned to wmiQuery. You are interested in only those printers that have a status of 1, 2, or 7. The Reference section of the script is seen below.

strComputer = "."
wmiNS = "\root\cimv2"
wmiQuery = "Select * from Win32_Printer" _
  & " Where PrinterStatus = 1" _
  & " or PrinterStatus = 2" _
  & " or PrinterStatus = 7"
Set objWMIService = GetObject("winmgmts:\\" _
  & strComputer & wmiNS)
Set colItems = objWMIService.ExecQuery(wmiQuery)

Output Information

If you tried to iterate through a collection that had no members, you would not receive a meaningful message. To avoid this, you add an If...Then...Else construction around the Output information section that appeared in the earlier script. If there are no printers with an error condition, the Count property of colItems will be zero. You use WScript.Echo to send a message to the console that all printers are fine. If, however, the count is not zero, you echo out the information used in the MonitorPrinterStatus.vbs script. The revised section looks like the following code:

If colItems.count = 0 Then
WScript.Echo "All printers are fine"
Else
For Each objItem in colItems
  WScript.Echo "Name: " & objItem.Name
  WScript.Echo "Location: " & objItem.Location
  WScript.Echo "Printer Status: " & funEvalStatus(objItem.printerStatus)
  WScript.Echo "Server Name: " & objItem.ServerName
  WScript.Echo "Share Name: " & objItem.ShareName
  WScript.Echo
Next
End If

Quick Check

Q.

What was required in the FilterPrinterStatus.vbs script to return only selected records from the WMI query?

A.

To return selected records, a Where clause was added to the WMI query.

Q.

What is needed in the FilterPrinterStatus.vbs script to ensure you have printers in your collection?

A.

To ensure you have printers in your collection, you used the Count property of collected items in the FilterPrinterStatus.vbs script.

Q.

What does a PrinterStatus code of 7 mean?

A.

A PrinterStatus code of 7 means the printer is offline.



Previous Page
Next Page