Tracking Script Progress in a Command Window

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

Running a script in a command window does not preclude you from using Internet Explorer as a way to track script progress; an Internet Explorer instance can be created and controlled from CScript in the same way it can be created and controlled from WScript.

At the same time, however, it is unusual for a command-line utility to generate a graphical progress indicator. Users might not recognize that the script running in the command window is tied to the progress indicator running in Internet Explorer. Because of this, you might want progress to be tracked in the same command window where the script is running.

Although you can track progress in a command window, there are at least two limitations:

  • There are few formatting options available to you beyond using blank spaces and a mixture of lowercase and uppercase letters.
  • You cannot erase the contents of the command window. For example, you might want to display a message such as, "Now completing Step 1 of 5." When this step is finished, you might want to erase the window and display, "Now completing Step 2 of 5." This cannot be done from a script. Even the cls command cannot be called from within a script; the command will run, but it will run within a new window that is automatically spawned. It will not clear the command window in which the script is running.

Of course, you can use Wscript.Echo to periodically display progress messages in a command window. For example, you might have script output that looks similar to this:

C:\Scripts>cscript services.vbs Now completing Step 1 of 5. Now completing Step 2 of 5. Now completing Step 3 of 5. Now completing Step 4 of 5. 

Another approach commonly used with command-line utilities is to display a series of dots (periods) as a script progresses:

C:\Scripts>cscript services.vbs Retrieving service information. This might take several minutes. ........................................................................ 

Each time the script performs a subtask, a new dot is written to the command window. However, this cannot be done by using Wscript.Echo. Each time you call Wscript.Echo, the value is written to a new line. Because of this, your progress indicator would look like this:

C:\Scripts>cscript services.vbs Retrieving service information. This might take several minutes. . . . . 

To append text to the command window, you must instead use the Wscript.StdOut.Write method and write each new value to the standard output stream.

Important

  • You can write to the standard output stream only if the script is running under CScript. If a script running under WScript attempts to write to the standard output stream, an "invalid handle" error will result.

Scripting Steps

Listing 17.28 contains a script that tracks script progress in a command window. To carry out this task, the script must perform the following steps:

  1. Echo the message, "Processing service information. This might take several minutes."
  2. Create a variable to specify the computer name.
  3. Use a GetObject call to connect to the WMI namespace root\cimv2, and set the impersonation level to "impersonate."
  4. Use the ExecQuery method to query the Win32_Service class.

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

  5. For each service in the collection, use the Write method to write a period (.) to the standard output stream.

    A production script would probably do additional processing during this stage (for example, saving service information to a database). In this sample script, service information is retrieved but no additional action is taken.

  6. Use the WriteLine method to write a blank line to the standard output.
  7. Echo the message, "Service information processed."

Listing 17.28   Tracking Script Progress in a Command Window

1 2 3 4 5 6 7 8 9 10 11 
Wscript.Echo "Processing information. This might take several minutes." strComputer = "." Set objWMIService = GetObject("winmgmts:" _     & "{impersonationLevel=impersonate}!\\" & strComputer& "\root\cimv2") Set colServices = objWMIService.ExecQuery _     ("SELECT * FROM Win32_Service") For Each objService in colServices     Wscript.StdOut.Write(".") Next Wscript.StdOut.WriteLine Wscript.Echo "Service information processed."

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