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:
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.
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:
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).
The setInterval method requires three parameters:
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.
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.
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.
This query returns a collection consisting of all the printers installed on the computer.
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.
Listing 13.3 Displaying Printer Status in a Web Page
|
|
Send us your feedback | « Previous | Next » |