Monitoring Printer Status in Real Time

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

The script shown in Listing 13.2 is probably best run as a scheduled task; otherwise, it will simply run once and then stop; it will not display the most current status unless you manually rerun it. By scheduling the script to run on a periodic basis (for example, every 15 20 minutes), you will be able to routinely view the current printer status in a command window. After you have viewed these results, you can close the command window and wait for the script to run again.

This script is less useful, however, if you want to have a real-time display of printer status, with a single window that periodically updates itself and shows the current printer status. This is due in large part to limitations on displaying output within a command window. Because there is no way to clear the command window programmatically, you cannot simply replace an old data set with a new data set. Instead, any updated printer status must be appended to the bottom of the old data set, resulting in a confusing display, particularly for print servers that manage scores of printers.

If you prefer to have a real-time display of printer status, you can display status information in a Web page rather than in a command window. There are several advantages to displaying results in a Web page:

  • It is easy to print or save the results: Simply use the Save or Print function built into your Web browser. The command window does not provide such a straightforward method for saving or printing data.
  • It is possible to create formatted output that provides visual cues regarding printer status. For example, you might use a green icon for a running printer and a red icon for a stopped printer. Formatting of this kind is impossible inside a command window.
  • It is easy to update the results. At the end of five minutes, the script can erase the previous status information and write the update status information in its place.

You can create a Web page to display printer status by combining WMI methods and VBScript functions with HTML tags. The best way to do this is by creating a Hypertext Application (HTA), a Web page saved with the .HTA file name extension rather than the more standard .HTM file name extension.

Although functionally equivalent to a Web page, HTAs are not bound by the tight security restrictions imposed by Microsoft® Internet Explorer. For example, if you attempt to run a WMI query from a standard .HTM Web page, you will receive a message informing you that WMI has not been marked as "safe for scripting" and asking whether you want to proceed. You can avoid this message and run the query without any problem simply by changing the file name extension from .HTM to .HTA.

Scripting Steps

Listing 13.3 contains the HTML tags and script code required to display printer status in a Web page. The code shown in this listing should be typed in Notepad or another text editor and then saved with the .HTA file name extension.

To carry out its task, the HTA must perform the following steps:

  1. Create a <SCRIPT> tag specifying VBScript as the scripting language.
  2. Create a window_onLoad procedure.

    Code in the window_onLoad procedure will automatically run whenever the window is loaded (either through initial startup, or by clicking the browser Refresh button).

  3. Use the setInterval method to create a timer that will update printer status every 60 seconds.

    The setInterval method requires three parameters:

    • GetInfo. The name of the procedure called by the timer.
    • 60000. Number of milliseconds between calls (60,000 milliseconds equals 60 seconds). The timer will call the GetInfo subroutine, wait 60 seconds, and then call the procedure again.
    • VBScript. Scripting language used by the GetInfo script.

    The setInterval method provides functionality similar to Wscript.Sleep: It allows the script to pause for a specified amount of time and then resume processing. This method must be used instead of Wscript.Sleep because Windows Script Host (WSH) methods cannot be called from within Internet Explorer.

  4. Create a subroutine named GetInfo that retrieves the current list of installed printers and displays status information in a table.

    To do this, the GetInfo subroutine must first delete the current version of the table (if one exists), re-create the table header, retrieve the list of printers, and then display the printer status in the table. It is easier to delete and re-create the table than to update individual rows within the table.

  5. Create a loop that deletes all the rows in the existing table (with the Table ID of objTable).

    Deleting the current table has the effect of wiping the page clean, allowing the latest status information to be displayed.

    The loop works backward from the last row in the table (Rows.Length 1) to the first row in the table (row 0). If there are three rows in the table, row 2 will be deleted first, then row 1, and then row 0.

  6. Create the table header. To do this, the script must:
    1. Use the InsertRow method to insert a new row in the table.
    2. Use the InsertCell method to insert a new cell in the table.
    3. Use the InnerText method to set the contents of the new cell to Name.
    4. Use the same procedures to insert cells labeled Location and Status.
  7. Create a variable to specify the computer name.
  8. Use a GetObject call to connect to the WMI namespace root\cimv2, and set the impersonation level to "impersonate."
  9. Use the ExecQuery method to query the Win32_Printer class.

    This query returns a collection consisting of all the printers installed on the computer.

  10. For each printer in the collection, create a new table row with three cells:
    • one cell to display the printer name
    • one cell to display the printer location
    • one cell to display the printer status

    Because printer status is returned as an integer, a Select Case statement is used to convert the value to a recognizable text string. For example, if the printer status is returned as 3, the word Idle will be displayed in the Web page.

  11. Use a <TABLE> tag with the ID objTable; this tag indicates the location of the table on the Web page. The Border = 1 parameter is included to place a border around each cell in the table.
  12. Use a <TBODY> tag with the ID objTableBody to represent the body of the table.
  13. Use the </TBODY> and </TABLE> tags to mark the end of the table.

Listing 13.3   Displaying Printer Status in a Web Page

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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 
<SCRIPT LANGUAGE = "VBScript"> Sub window_onLoad     GetInfo     iTimerID = window.setInterval("GetInfo", 60000, "VBScript") End Sub Sub GetInfo     For i = (objTable.Rows.Length - 1) to 0 Step -1         myNewRow = Document.All.objTable.deleteRow(i)     Next     Set objRow = objTableBody.InsertRow()     objRow.Style.fontWeight = "bold"     Set objCell = objRow.InsertCell()     objCell.InnerText = "Name"     Set objCell = objRow.InsertCell()     objCell.InnerText = "Location"     Set objCell = objRow.InsertCell()     objCell.InnerText = "Status"     strComputer = "."     Set objWMIService = GetObject("winmgmts:" _         & "{impersonationLevel=impersonate}!\\" & _             strComputer & "\root\cimv2")     Set colPrinters =  objWMIService.ExecQuery _         ("SELECT * FROM Win32_Printer")       For Each objPrinter in colPrinters         Set objRow = objTableBody.InsertRow()         Set objCell = objRow.InsertCell()         objCell.InnerText = objPrinter.Name         Set objCell = objRow.InsertCell()         objCell.InnerText = objPrinter.Location         Set objCell = objRow.InsertCell()         Select Case objPrinter.PrinterStatus             Case 1                 strPrinterStatus = "Other"             Case 2                 strPrinterStatus = "Unknown"             Case 3                 strPrinterStatus = "Idle"             Case 4                 strPrinterStatus = "Printing"             Case 5                 strPrinterStatus = "Warming up"         End Select         objCell.InnerText = strPrinterStatus     Next End Sub </SCRIPT> <TABLE ID = "objTable" border = "1" > <TBODY ID = "objTableBody"> </TBODY> </TABLE>

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