Displaying Data in a Web Page

Microsoft® Windows® 2000 Scripting Guide

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>" 

Scripting Steps

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:

  1. Create an instance of Internet Explorer.
  2. Open a blank Web page by navigating to "about:blank".
  3. Configure various Internet Explorer properties, such as the width and height of the window, and hide items such as the toolbar and status bar.
  4. Set the Visible property to 1 to display the instance on the screen.
  5. Create an object reference to the Document object.
  6. Prepare the Web page for writing by opening the Document object (objDocument.Open).
  7. Use HTML tags to:
    • Set the page title.
    • Set the background color (bgcolor) to white.
    • Insert a table that fills 100% of the document width.
    • Create the initial table row with two columns: one labeled Service and the other labeled State.
  8. Create a variable to specify the computer name.
  9. Use a GetObject call to connect to the WMI namespace root\cimv2, and set the impersonation level to "impersonate."
  10. Use the ExecQuery method to query the Win32_Service class.

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

  11. For each service in the collection, create a new row in the table. Each row has two columns, one containing the service name and the other containing the service state.
  12. Use HTML tags to mark the end of the table, the document body, and the document head.

Listing 17.7   Displaying Data 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 
Set objExplorer = CreateObject("InternetExplorer.Application") objExplorer.Navigate "about:blank"   objExplorer.ToolBar = 0 objExplorer.StatusBar = 0 objExplorer.Width = 800 objExplorer.Height = 570 objExplorer.Left = 0 objExplorer.Top = 0 objExplorer.Visible = 1             Do While (objExplorer.Busy) Loop    Set objDocument = objExplorer.Document     objDocument.Open                   objDocument.Writeln "<html><head><title>Service Status</title></head>" objDocument.Writeln "<body bgcolor='white'>" objDocument.Writeln "<table width='100%'>" objDocument.Writeln "<tr>" objDocument.Writeln "<td width='50%'><b>Service</b></td>" objDocument.Writeln "<td width='50%'><b>State</b></td>" objDocument.Writeln "</tr>" strComputer = "." Set objWMIService = GetObject("winmgmts:" _     & "{impersonationLevel=impersonate}!\\" & strComputer& "\root\cimv2") Set colServices = objWMIService.ExecQuery _     ("SELECT * FROM Win32_Service") For Each objService in colServices     objDocument.Writeln "<tr>"     objDocument.Writeln "<td width='50%'>" & objService.DisplayName & "</td>"     objDocument.writeln "<td width='50%'>" & objService.State & "</td>"     objDocument.Writeln "</tr>" Next objDocument.Writeln "</table>" objDocument.Writeln "</body></html>" objDocument.Write() objDocument.Close      

Clearing 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 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