Previous Page
Next Page

Obtaining the Status of Printers

In 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:

MonitorPrinterStatus.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"
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 Information

The 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.

Table 18-2. Variables for MonitorPrinterStatus.vbs

Variable

Use

strComputer

Holds the target computer

wmiNS

Holds the WMI namespace that will be connected to

wmiQuery

Holds the WMI query that will be executed

objWMIService

Holds the connection to WMI

colItems

Holds the collection that comes back as a result of the WMI query

objItem

Placeholder that allows us to iterate through the collection of items that was returned by the WMI query


Reference Information

The 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 Information

The 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 Information

Once 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

Quick Check

Q.

What WMI class provides more than 80 properties for managing printers?

A.

The Win32_Printer class provides more than 80 properties for managing printers.

Q.

What is needed to obtain meaningful information from the PrinterStatus property?

A.

To obtain meaningful information from the PrinterStatus property, you must interpret the status codes.

Q.

When using the Win32_Printer class, how is the data returned?

A.

When using the Win32_Printer class, the data is returned as a collection of printer objects.



Previous Page
Next Page