Monitoring Status of Remotely Running Scripts
Microsoft® Windows® 2000 Scripting Guide
Running scripts on remote computers is most useful if you can be sure that the scripts actually ran successfully. You can do so by using the WshRemote object and monitoring events that are generated when the remote script runs.
The WshRemote object (created when you call the CreateScript method) has a property named Status that monitors the status of the worker script associated with the object. The Status property can take on one of three values:
0 indicates that the remote script has not yet started to run.
1 indicates that the remote script is currently running.
2 indicates that the remote script has finished running.
In addition to the WshRemote object Status property, three events are triggered when certain things happen with the remote script. Having your script handle these events allows you to monitor the running of the remote script. The three events are:
Start. This event is triggered when the remote script starts executing.
End. This event is triggered when the remote script is finished executing.
Error. This event is triggered only if the remote script encounters a problem. If the Error event is triggered, you can access the WshRemoteError object as a property of the WshRemote object and retrieve information about the error that occurred.
The script in Listing 3.43 has three subroutines (Remote_Start, Remote_Error, and Remote_End) that respond to the three events Start, Error, and End. The subroutines are called if and when each of the events takes place. In turn, each subroutine simply displays a message indicating that the corresponding event has taken place.
The call to ConnectObject in line 7 of Listing 3.43 causes the script to handle the events. Note that the string "Remote_", passed as the second parameter to the ConnectObject method, determines the first part of the names of the event-handling subroutines; the last part of the names is determined by the particular event the subroutine handles.
Listing 3.43 Monitoring a Remotely Running Script
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
strRemoteComputer = "RASServer01" strWorkerScript = "CreateTextFilMapNetworkDrive.vbs" Set objWshController = WScript.CreateObject("WshController") Set objRemoteScript =_ objWshController.CreateScript(strWorkerScript, strRemoteComputer) Wscript.ConnectObject objRemoteScript, "Remote_" objRemoteScript.Execute Do While Not objRemoteScript.Status = 2 Wscript.Sleep(100) Loop Sub Remote_Start Wscript.Echo "Started Running Remote Script." End Sub Sub Remote_Error Wscript.Echo "Error Running Remote Script." objRemoteScript.Terminate Wscript.Quit End Sub Sub Remote_End Wscript.Echo "Finished Running Remote Script." End Sub
If everything works as expected, only the Start and End events will be triggered when you run the script in Listing 3.43. When the script runs, you will receive messages indicating that the remote script started and ended. If an error occurs, however, you will receive a simple message stating that an error occurred. For information about retrieving more details about errors, see "Examining Errors Produced by Remotely Running Scripts", which follows.