Creating a Filtered Print MonitorOne 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 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 InformationThe 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 InformationIf 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
![]() |