Obtaining the Status of PrintersIn your first printer management script, you'll use the Win32_Printer WMI class to obtain information about the status of printers defined on a computer. This particular script runs on Windows Server 2003 and on Windows XP, so it can run on a server to obtain the status of all the printers defined, or it can run as a diagnostic tool on a workstation. The MonitorPrinterStatus.vbs script follows: 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" Set objWMIService = GetObject("winmgmts:\\" _ & strComputer & wmiNS) Set colItems = objWMIService.ExecQuery(wmiQuery) 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 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 Header InformationThe Header information section of the script does not perform any real magic. You begin with Option Explicit so that you're forced to keep track of your variables. Next you have On Error Resume Next, and then you have six variables. A description of the variables appears in Table 18-2.
Reference InformationThe Reference information section of the script is used to assign values to some of the variables that were declared in the Header information section of the script. You use the period inside a set of double quotation marks to represent the local machine and assign it to strComputer. If you wanted to run the script against other computers, you could substitute their names for the period. The root\cimv2 namespace is assigned to the variable wmiNS. You use "Select * from Win32_Printer" to return everything from the Win32_Printer class. Though easy to do, this is not the most efficient way to gather your information, which is somewhat of an issue when working with Win32_Printer because it is a rather large class. Your reference to the system's WMI service is objWMIService. You use the winmgmts moniker to simplify the connection process. The last reference information that needs to be set is using the ExecQuery method of objWMIService to execute the query represented by the variable wmiQuery. The Reference section is seen below. strComputer = "." wmiNS = "\root\cimv2" wmiQuery = "Select * from Win32_Printer" Set objWMIService = GetObject("winmgmts:\\" _ & strComputer & wmiNS) Set colItems = objWMIService.ExecQuery(wmiQuery) Worker InformationThe Worker information section of the MonitorPrinterStatus.vbs script consists of a single function called funEvalStatus. The funEvalStatus routine is used to translate the status code that is returned by the PrinterStatus property into a more meaningful message. To do the matching, you use a Select Case construction that looks for a match with one of the seven possible return status codes. The Worker section is seen below. 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 Output InformationOnce you work through matching the return status codes with a more meaningful status message, it is time to echo out the information. You use a For Each...Next construction to iterate through the collection of items that was returned by the WMI query. You use WScript.Echo to echo out a few of the more than 80 properties available via the Win32_Printer class. Because both the Name and the Location properties are simple string data, you can echo them out directly. However, to properly interpret the printer status code, you need to enter the subEvalStatus subroutine. You come out of that subroutine with a meaningful status message, and so you echo that out as well. Finally, you echo out the server name and the printer share name. 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
![]() |