Displaying Current Process Performance Data

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

Although many system administration scripts run in a command window, the command window is not always suitable for displaying process performance data. Within a command window, there are few options for formatting data: you are limited to a single nonproportional font, with each character the same size and color. In addition, data within the command window cannot be cleared by using a script. Neither Windows Script Host (WSH) nor VBScript provides methods for clearing the screen; although you can call the command-line utility cls from within a script, instead of the current command window being cleared a second command window opens, and the cls command is carried out there.

The inability to clear a command window has important implications for scripts that monitor process status or process performance. You cannot display the current process status, wait a few minutes, and then erase that data and replace it with updated status information. Instead, each time you retrieve process information, the new data is appended to the end of the existing data. While this large collection of data might be useful for viewing process performance over time, it is far less useful if you simply want to view the current state of each process running on a computer.

As an alternative to displaying process performance in a command window, you can use a hypertext application (HTA) that allows you to display process information on a Web page. Displaying process data in an HTA offers the following advantages:

  • You can clear the page each time the latest set of data is retrieved so that the page displays only the current process information.
  • You can use additional formatting to highlight important items. For example, you might use a different font color to indicate a process that has a working set size greater than a specified amount.

An HTA is a Web page that has the file name extension .HTA. HTAs are useful for script writers because they can use Automation objects that have not been marked as safe for scripting. This allows you to use WMI within a Web page.

For more information about HTAs, see "Creating Enterprise Scripts" in this book.

Scripting Steps

Listing 14.7 contains the HTML tags and VBScript code required to create an HTA that displays process performance information on a Web page. Type this information into a text editor, and then save it with the .HTA file name extension.

To carry out the task of displaying process performance information on a Web page, the HTA must perform the following steps:

  1. Create a <SCRIPT> tag specifying VBScript as the scripting language.
  2. Create a constant named USE_COMMAS.

    This constant is used to format the working set value for each process.

  3. Create a window_onLoad subroutine.

    Code in the window_onLoad subroutine automatically runs whenever the window is loaded (either through initial startup or by clicking the browser Refresh button).

  4. Use the setInterval method to create a timer that updates the list of running processes every 10 seconds.

    The setInterval method requires three parameters:

    • GetInfo. The name of the subroutine called by the timer.
    • 10000. Number of milliseconds between calls. The timer calls the GetInfo subroutine, waits 10 seconds, and then calls the subroutine again.
    • VBScript. Scripting language used by the GetInfo script.

    The setInterval method provides functionality similar to Wscript.Sleep: It allows you to pause the script 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 Internet Explorer.

  5. Create a subroutine named GetInfo that retrieves the current set of processes and displays the process information in a table.

    To do this, the GetInfo subroutine must first delete the current version of the table (if one exists), create the table header, retrieve the process information, and then display the process information in the table.

  6. 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 process 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 is deleted first, then row 1, and then row 0.

  7. Create the table header.

    To do this, the script must:

    • Use the InsertRow method to insert a new row in the table.
    • Use the InsertCell method to insert a new cell in the table.
    • Use the InnerText method to set the contents of the new cell to Process ID.
    • Use the same procedures to insert cells labeled Process Name and Working Set Size.
  8. Create a variable to specify the computer name.
  9. Use a GetObject call to connect to the WMI namespace root\cimv2 on the computer, and set the impersonation level to "impersonate."
  10. Use the ExecQuery method to query the Win32_Process class. This query returns a collection consisting of all the processes running on the computer.
  11. For each process in the collection, create a new row with three cells to display the following: process ID, process name, and working set size.

    The FormatNumber function is used to insert comma delimiters in the working set values. By using this function, numbers are displayed with thousands separators, such as 3,456,789 rather than 3456789. The FormatNumber function requires the following parameters:

    • StrProcess.WorkingSetSize. Value to be formatted.
    • 0. Indicates the number of digits displayed after the decimal point. A 0 indicates that the decimal point should not be used.
    • Empty. Used to display a leading 0 for fractional values. Not applicable for this script.
    • Empty. Used to place parentheses around negative values. Not applicable for this script.
    • USE_COMMAS. Constant that indicates whether numbers are grouped using the group delimiter specified in Control Panel. For U.S. English, this is typically the comma. You can find the default delimiter for a computer by opening the Regional and Language Options Control Panel, clicking the Numbers tab, and then checking the value in the Digit grouping symbol box.
  12. Create a <TABLE> tag with the ID objTable. In addition, include the Border = 1 parameter to place a border around each cell in the table.
  13. Create a <TBODY> tag with the ID objTableBody.

    This tag is used to represent the actual body of the table.

Listing 14.7   Displaying Process Performance on 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 
<SCRIPT LANGUAGE = "VBScript"> Sub window_onLoad     iTimerID = window.setInterval("GetInfo", 10000, "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 = "Process ID"     Set objCell = objRow.InsertCell()     objCell.InnerText = "Process Name"     Set objCell = objRow.InsertCell()     objCell.InnerText = "Working Set Size"     strComputer = "."     Set objWMIService = GetObject("winmgmts:" _         & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")     Set colProcesses = objWMIService.ExecQuery _         ("SELECT * FROM Win32_Process")     For Each strProcess in colProcesses         Set objRow = objTableBody.InsertRow()         Set objCell = objRow.InsertCell()         objCell.InnerText = strProcess.ProcessID         Set objCell = objRow.InsertCell()         objCell.InnerText = strProcess.Name         Set objCell = objRow.InsertCell()         objCell.InnerText = FormatNumber(strProcess.WorkingSetSize,0,,,-1)     Next End Sub </SCRIPT> <TABLE ID = "objTable"> <TBODY ID = "objTableBody"> </TBODY> </TABLE>

The finished HTA appears similar to Figure 14.2. Every 10 seconds, the display is replaced with updated process information.

Figure 14.2   Process Performance Data Displayed on a Web Page

Process Performance Data Displayed on a Web Page


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