Tracking Script Progress by Using Internet Explorer

Microsoft® Windows® 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous | Next »   

If you feel that a script needs a progress indicator, it is recommended that you use a graphical progress indicator; after all, if visual cues are important, your progress indicator should be visually compelling. Perhaps the easiest way to add a graphical progress indicator to your scripts is to use Internet Explorer.

By the way, this is true even if the script runs in a command window under CScript; Internet Explorer can be instantiated and used as a progress indicator even if the script is running from the command line.

One way to indicate progress by using Internet Explorer is to display a message in the browser. In some cases, you might want to display a single message ("Please wait .") and then have the message disappear when the script is finished. Alternatively, you might want to periodically change the message to reflect whatever the script is doing. For example, if you have a script that stopped a service, waited two minutes, and then restarted the service, you might display these messages in the browser:

  • Stopping service .
  • Waiting two minutes before restarting service .
  • Restarting service .

Regardless of the number or the type of messages you choose to display, you can display custom messages within Internet Explorer by configuring the InnerHTML property of the document body. This replaces the entire document body with the specified text. For example, this code clears the document and displays the message, "Service retrieval in progress.":

objExplorer.Document.Body.InnerHTML = "Service retrieval in progress." 

You can also use standard HTML tags to include formatting when setting the InnerHTML property. For example, this code uses the < B> tag to display the message in bold:

objExplorer.Document.Body.InnerHTML = "<B>Service information retrieved.</B>" 

There are two important points to keep in mind when using Internet Explorer as a progress indicator:

  • Closing Internet Explorer will not necessarily terminate the script. However, it is possible to code your script so that closing Internet Explorer will stop the script from running.
  • If Internet Explorer is closed and your script attempts to manipulate the nonexistent object, an error will occur.

To prevent this, use On Error Resume Next statement within the script. Anytime you attempt to manipulate Internet Explorer, check the error number. If the error number is anything other than 0, that means a problem occurred, and it is likely that Internet Explorer is no longer available. In that case, you must decide whether you want your script to terminate itself, continue without a progress indicator, or generate a new instance of Internet Explorer.

For information about a way to overcome these problems, see "Stopping a Script When Internet Explorer Is Closed" in this chapter.

Scripting Steps

Listing 17.25 contains a script that tracks script progress by using Internet Explorer. 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 the user interface settings, and then show Internet Explorer by setting the Visible property to True.
  4. Set the InnerHTML property of the document to the message "Retrieving service information. This might take several minutes to complete."
  5. Use a GetObject call to connect to the WMI namespace root\cimv2, and set the impersonation level to "impersonate."
  6. Use the ExecQuery method to query the Win32_Service class.

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

  7. For each service in the collection, pause for 200 milliseconds. This is done simply to ensure that the progress indicator remains on the screen long enough for you to view it.
  8. Set the InnerHTML property of the document to the message "Service information retrieved."
  9. Pause for 3 seconds (3,000 milliseconds). This pause is inserted simply to give the user a chance to see the message that service information has been retrieved.
  10. Use the Quit method to dismiss Internet Explorer.

Listing 17.25   Tracking Script Progress by Using Internet Explorer

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 
Set objExplorer = CreateObject("InternetExplorer.Application") objExplorer.Navigate "about:blank"   objExplorer.ToolBar = 0 objExplorer.StatusBar = 0 objExplorer.Width = 400 objExplorer.Height = 200 objExplorer.Left = 0 objExplorer.Top = 0 Do While (objExplorer.Busy)     Wscript.Sleep 200 Loop    objExplorer.Visible = 1             objExplorer.Document.Body.InnerHTML = "Retrieving service information. " _     & "This might take several minutes to complete." strComputer = "." Set objWMIService = GetObject("winmgmts:" _     & "{impersonationLevel=impersonate}!\\" & strComputer& "\root\cimv2") Set colServices = objWMIService.ExecQuery _     ("SELECT * FROM Win32_Service") For Each objService in colServices     Wscript.Sleep 200 Next objExplorer.Document.Body.InnerHTML = "Service information retrieved." Wscript.Sleep 3000 Wscript.Quit

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