Microsoft® Windows® 2000 Scripting Guide
« Previous | Next »
Web pages provide an alternative to displaying script output in the command window. Unlike the command window, Web pages support a wide array of formatting options, including color, fonts, and graphics. This lets you display script output in a manner that is much easier to decipher at a glance than output displayed in the command window. For example, you might use red to format the status of any services that are stopped. That way, an administrator can verify that all services are up and running without having to stop and read through the entire display. Figure 17.2 shows a simple example of how you might display service status information.
Figure 17.2 Data Displayed in a Web Page
Data Displayed in a Web Page
Perhaps the easiest way to display real-time data is to use the WriteLn method. With this method, you can use a script to open a blank Web page and then send HTML code to the page. Each time you send information to the Web page by using WriteLn, that information is appended to the end of the page. For example, this code writes the numbers 1 through 5 to a Web page, without any formatting of any kind:
objDocument.Writeln
"1
"objDocument.Writeln
"2
"objDocument.Writeln
"3
"objDocument.Writeln
"4
"objDocument.Writeln
"5
"
The resulting Web page will look like this:
12345
To place each number on a separate line, include the HTML tag <BR> as part of the WriteLn parameter:
objDocument.Writeln
"1<BR>
"objDocument.Writeln
"2<BR>
"objDocument.Writeln
"3<BR>
"objDocument.Writeln
"4<BR>
"objDocument.Writeln
"5<BR>
"
The revised Web page will look like this:
1
2
3
4
5
You can include any HTML tags as part of the WriteLn parameter; in that regard, dynamically creating a Web page is no different than using a text editor to write an .htm file. For example, to write text formatted with the <H2> heading level, use code similar to this:
objDocument.Writeln
"<H2>This is an H2 heading</H2>
"
Listing 17.7 contains a script that displays data in a Web page. To carry out this task, the script must perform the following steps:
This query returns a collection consisting of all the services installed on the computer.
Listing 17.7 Displaying Data in a Web Page
|
|
The WriteLn method allows you to add information to a Web page: Each time you call the WriteLn method, the new data is appended to the bottom of the page. This works fine in many situations. For example, if you want to extract a particular set of records from an event log, you can display those records in a Web page by using WriteLn. The fact that WriteLn appends these records one right after the other is no problem in fact, that is exactly what you want it to do.
In other cases, however, you might want to periodically clear the screen to make room for new information. For example, if you have a script that monitors printer status every 15 minutes, you want the initial printer status displayed in the Web page. When the printer status is resampled, you probably do not want the second set of data to be appended to the end of the first set. Instead, you want to clear the browser screen and display only the current set of data.
One quick way to clear the browser screen is to create a TextRange object that encompasses the entire document and then set the Text property of that object to nothing. A TextRange object represents text within an HTML element; these elements include the document body, a button, a text box, and any other element that has a Text property.
For example, this code creates a TextRange object that encompasses the entire body of the document and then clears that object by setting the Text property to an empty string (""):
Set objTextRange = objDocument.body.CreateTextRange() objTextRange.Text =
""
Send us your feedback | « Previous | Next » |