Guy's Scripting Ezine 46 - VBScript MsgBox
VBScript MsgBox Examples
Early in my scripting career, I thought that MsgBox and WScript.echo were practically one and the same. Wrong. Worse still, I used to believe that when it came to generating message boxes, MsgBox was inferior to WScript.echo - wrong again. Please learn from my mistakes, and open your mind to employing MsgBox, especially in situations where you are dealing with users making choices.
Guy Recommends: The Free IP Address Tracker (IPAT)
Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets. IPAT cracks this problem of allocating IP addresses in networks in two ways:
For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges.
For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker
I now realise that in terms of features, MsgBox is superior to WScript.echo. The first point to note is that unlike WScript.echo, MsgBox is a function. The significance is that MsgBox can return a variety of values depending on which dialog button you press. Therefore, MsgBox opens up exciting possibility for scripting, let us study an example.
The purpose of this example is to introduce WMI (Windows Management Interface) which will act as a vehicle for our MsgBox experiments.
Scenario, you want a script to provide information about the
We can use WMI command to retrieve
information about the computer. For example:
In truth, the MsgBox function is not really needed for our first script. However, please bear with me, because later examples will be more interesting and complex. Remember that this example is better than the usual 'Hello World' introductions.
Instructions for VBScript MsgBox () Function
Note 1: There is only a single OK Button! Later examples will have the additional buttons: 'No' and 'Cancel'.
Note 2: Numeric 1 is the return value. So the
Note 3: The script also employs a loop: For Each .... Next. Collation items such as objItem.Version are properties of the computer, which are extracted by the WMI part of the script.
SolarWinds' Orion performance monitor will help you discover what's happening on your network. This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.
What I like best is the way NPM suggests solutions to network problems. Its also has the ability to monitor the health of individual VMware virtual machines. If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.
Now it's time for VBScript to build on the first MsgBox example. We will also dissect the MsgBox() function.
MsgBox(prompt[, buttons][, title])
I will employ the 'Prompt' argument to display text in the Message box. 'Prompt' tells the user what the message box will achieve.
The most interesting MsgBox argument is - 'buttons'. The default value of 0 (zero) means just displays the OK button. If we change the value of buttons from 0 to 3 then we get two extra buttons, 'No' and 'Cancel'.
Example A: Buttons is assumed to be 0, the default value.
strMbox = MsgBox("Guy says you must select the LocalHost!")
Example B: Buttons is explicitly set to 3 (and title is added).
strMbox = MsgBox("Do you want the LocalHost?", 3 ,"Hostname")
When scripting with MsgBox, there are two numbers to be aware of, firstly there is the input or display 'Button' number, which controls the actual Message Box. Secondly, there is a separate output or return value number which we do not normally see. However, this return value can be acted upon by subsequent lines of the script.
Naturally, what happens to the output depends on which button you press, this is because unlike WScript.echo, MsgBox() is a function, and functions return values. Clicking the Yes button results in a return value of 6.
To give the script some extra beef, I have introduced an Else statement with an Inputbox which fields all return values that are not equal to 6.
Note 1: If strMbox = 6, then WMI processes the LocalHost and returns its operating system data.
Other return values for MsgBox() are 1 = OK, 2 = Cancel, 7 = No. (And of course, 6 = Yes)
Note 2: If the user clicks NO, or Cancel then strMbox is not 6, so an Inputbox is displayed.
Challenge: Set the 'Buttons' value to 4. That should just display Yes and No (Removes the Cancel Button).
Here is the line to edit:
MsgBox is just the ticket for scripts where you need simple user confirmation. Under the covers, the MsgBox() function supplies a rich set of scripting input and output values. These return values act as branches for your underlying script.
See more about VBScript techniques