Guy’s Scripting Ezine 130 – Using VBScript Exit Codes

Guy’s Scripting Ezine 130 – Using VBScript Exit Codes

While these scripts have an important purpose, namely to master exit codes, they are also an ideal vehicle for those with little experience, who wish to learn VBScript by experimenting with an easy-to-understand example.  Follow my lead and go from zero to hero in 10 minutes.

 ♣

This Week’s Secret

Exit codes have been my Achilles’ heel.  Naturally I have been aware of their existence, but I have just not had a killer reason to include this property in my VBScripts.  Consequently, when Keith Hubbard wrote in asking for an exit code example, my script cupboard was bare.

What made my day was when Keith Hubbard wrote again to report that he had solved his own problem, moreover, he kindly enclosed a completed example illustrating exit code.

This Week’s Mission

The whole point of trapping the exit code (.status =1) is so that your script will wait for one event to complete before continuing with the next section of code.  Not only is this good programming practice, but also it solves the problem of dependency.

One other technique to look out for, is the loop that keeps testing: ‘Has the program exited yet?’.  As may already know, I preach building scripts gradually, thus this script just focuses on one task, waiting for an event to occur.  In the production script instead of a WScript.echo message, we would write code to delivering the main payload of our project.  Keith also points out that this technique of waiting for the exit code is efficient because it saves CPU cycles and memory.

Guy Recommends: SolarWinds Free Wake-On-LAN UtilitySolarwinds Wake-On-LAN

Encouraging computers to sleep when they’re not in use is a great idea – until you are away from your desk and need a file on that remote sleeping machine!

WOL also has business uses for example, rousing machines so that they can have update patches applied.  My real reason for recommending you download this free tool is because it’s so much fun sending those ‘Magic Packets’. Give WOL a try – it’s free.

Download your free copy of SolarWinds Wake-On-LAN

Example 1 – VBScript Exit Code .Status

Pre-requisites

  1. You need a machine with the Calc (Calculator) program installed.  Alternatively, edit "Calc" to "Notepad", or any executable that you can launch via the Run dialog box.

Instructions to test the exit code .Status

  1. Save the following script with a .vbs extension, for example exit.vbs
  2. Navigate to the folder where you saved the .vbs file, double click the file.
  3. Note a good VBScript editor will help you identify VBScript methods and properties, for example .status, .exit and .quit.  Get your free trial of OnScript

‘ VBScript to demonstrate exit code.
‘ Authors Keith Hubbard and Guy Thomas
‘ Version 1.2 – November 2006
Option Explicit
Dim objShell, objExec
Set objShell = CreateObject("WScript.Shell")
Set objExec = objShell.Exec("Calc")
    Do While objExec.Status = 0
WScript.Sleep 100
Loop
WScript.Echo "Calc Closed Status = " & objExec.Status
WScript.Quit

Learning Points

Note 1:  After the header, the script creates a Shell object called objShell.  From this master object we create objExec which actually launches the program.

Note 2:  Pay close attention to the .Exec method as it underpins this script.  There a number of ways to launch a program, but the .Exec method brings the .Status method into play.

Note 3:  objExec.Status obtains the value for the exit code.  A value of zero means still open, hence the script loops until the value changes from zero to one.

Note 4:  This script waits or sleeps for 100 milliseconds.  In example 2 I chose a value of 500 meaning half a second.  Feel free to experiment with WScript.Sleep 100.

Note 5:  For this test script, the code lacks a realistic payload.  WScript.echo does not perform any useful work, however, we will improve on this limitation in Example 2 below.

Note 6: Thanks to Tom Riedy, Version 1.2 now has Option Explicit added.  This exposed a DIM error which we have now corrected.

Guy Recommends: Tools4ever’s UMRAUMRA The User Management Resource Administrator

Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.

It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.

Example 2 – Exit Code (Binocular Vision)

It is my belief that if you find two ways of doing the same thing – not difficult with VBScript – then you get what I call binocular vision and thus achieve better understanding of VBScript’s methods and syntax.

What this script does is introduce variables to control the applications.  Moreover there is a minor payload in that when you close the first program (Calc), the second program (Notepad) launches.  However, Notepad always waits for as long as it takes Calc to close, before executing.

Instructions

  1. Save the following script with a .vbs extension, for example exit2.vbs
  2. Navigate to the folder where you saved the .vbs file, double click the file.
  3. Note a good VBScript editor will help you with the line numbers.  Get your free trial of OnScript

‘ VBScript to demonstrate waiting with exit code.
‘ Authors Keith Hubbard and Guy Thomas
‘ Version 2.2 – November 2006
Option Explicit
Dim objShell, objExec, strPrelim, strMainProg
strPrelim = "Calc"
strMainProg ="Notepad"
Set objShell = CreateObject("WScript.Shell")
Set objExec = objShell.Exec(strPrelim)
Do While objExec.Status = 0
    WScript.Sleep 500
Loop
WScript.Echo strPrelim & " just closed. Click to launch "_
& strMainProg
Set objExec = objShell.Exec(strMainProg)
WScript.Quit

Learning Points

Note 1:  My biggest worry is that the underscore (_) will not work properly when you copy my example.  What the (_) does is to allow one instruction to word-wrap over two lines.  Any problem with line 13, then substitute this:
WScript.Echo strPrelim & " just closed. Click to launch " & strMainProg

Note 2:  Observe how the script introduces two variables strPrelim and strMainProg

Note 3:  Study this sequence, a) strPrelim (Calc) executes, b) then the Do While loop checks the status.  c) Only when the status changes from zero to one does the payload strMainProg execute.

Note 4: Tom Riedy prompted me to add Option Explicit.  As a result the above version number is 2.2, moreover the variables objShell, objExec, are now declared correctly, and I have removed objCalc as it was not doing anything useful.

Guy’s Challenges

Challenge 1:  Study and trace strPrelim and strMainProg.  If necessary amend their values to launch different programs in your VBScript.

Challenge 2:  Alter the loop logic.  Tell it to Loop UNTIL the value =1.

Challenge 3: On a production script you could change, or remove the WScript.Echo command.

Summary of VBScript Exit Codes

Objects have properties.  The .Status property can have a value of zero when a program is running, but when it closes the value changes to one.  A neat script can trap the value of the exit code and then take appropriate action.  In practical terms mastering the exit codes means your scripts can wait for one action to complete before the next program launches.  This logical, measured code makes for an orderly, well behaved script – just what we want.

See more about VBScript error correcting codes

VBScripts  • 800 Code list  • Ezines  • Logon Scripts   • PowerShell ErrorAction  • VM Monitor

Ezine 72 Error correcting  • Ezine 107 Error correcting  • Tool Kit   • WMI Monitor Review

Ezine 96 Errors  • Ezine 117 Troubleshooting pure  • Ezine 130 VBScript codes