Is your server running slowly? Check with SolarWinds ipMonitor
Get a free evaluation copy of ipMonitor
Contents for Guy's Scripting Ezine 46 - MsgBox
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.
MsgBox
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
operating system.
We can use WMI command to retrieve
information about the computer. For example: Set objWMIService =
GetObject("winmgmts: While we could write the results to a file,
I would prefer to display the WMI information on screen in a message box.
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 MsgBox () Function
- Copy and paste the script below into notepad.
- Save the file with .vbs extension e.g. MessageBox.vbs.
- Double click and examine the message box.
' MessageBox.vbs
' MsgBox Example VBScript to generate a message box.
' Author Guy Thomas http://computerperformance.co.uk/
' Version 1.3 - September 19th 2004
' --------------------------------------
Option Explicit
Dim objWMIService, objItem, colItems
Dim strComputer, strMbox
'On Error Resume Next
strMbox = "."
strMbox = MsgBox("Guy says you must select the localhost!")
If strMbox = 1 Then
strMbox ="."
End If
Set objWMIService = GetObject("winmgmts:\\" & strMbox & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from
Win32_OperatingSystem",,48)
For Each objItem in colItems
Wscript.Echo "HostName: " & vbTab & objItem.CSName & vbCr _
& "----------------------------------" & vbCr _
& "Operating System: " & vbTab & objItem.Caption & vbCr _
& "Version: " & vbTab & vbTab & objItem.Version & vbCr _
& "System Device: "& vbTab & objItem.SystemDevice & vbCr _
& ""
Next
WScript.Quit
' End of example VBScript
Learning Points
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
statement: If strMbox = 1 Then strMbox = "." Sets the WMI script to
interrogate the local host. Again this idea of using the return value
is to prepare the ground for more complex scripts.
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.
Now it's time to build on the first MsgBox example and to 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.
' MessageBox3.vbs
' VBScript to generate a message box.
' Author Guy Thomas http://computerperformance.co.uk/
' Version 2.2 - September 19th 2004
' --------------------------------------
Option Explicit
Dim objWMIService, objItem, colItems
Dim strHost, strMbox
' On Error Resume Next
strMbox = "."
strMbox = MsgBox("Would you like WMI data on localhost?",3,"Hostname")
If strMbox = 6 Then
strHost ="."
Else
strHost = InputBox("Enter your prefered Hostname","Hostname","LocalHost")
End If
Set objWMIService = GetObject("winmgmts:\\" & strHost & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from
Win32_OperatingSystem",,48)
For Each objItem in colItems
Wscript.Echo "HostName: " & vbTab & objItem.CSName & vbCr _
& "----------------------------------" & vbCr _
& "Operating System: " & vbTab & objItem.Caption & vbCr _
& "Version: " & vbTab & vbTab & objItem.Version & vbCr _
& "Build Number: " & vbTab & objItem.BuildNumber & vbCr _
& "Build Type: " & vbTab & objItem.BuildType & vbCr _
& "Serial Number: "& vbTab & objItem.SerialNumber & vbCr _
& "Product Type: "& vbTab & objItem.ProductType & vbCr _
& "OSLanguage: " & vbTab& objItem.OSLanguage & vbCr _
& "Boot Device: " & vbTab & objItem.BootDevice & vbCr _
& "System Directory: "& vbTab & objItem.SystemDirectory & vbCr _
& "System Drive: " & vbTab & objItem.SystemDrive & vbCr _
& "SPack Major: "& vbTab & objItem.ServicePackMajorVersion & vbCr _
& "SPack Minor: "& vbTab & objItem.ServicePackMinorVersion & vbCr _
& ""
Next
WScript.Quit
' End of example MsgBox VBScript
Learning Points
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: strMbox = MsgBox("Would you like WMI data on LocalHost?",3,"Hostname")
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.
Their topics and material are ideal for getting you started with VBScript. The
videos are easy to follow and you can control the pace. Try their free demo material and then see if you want to buy the full package.
See more about VB Script Training CD.
|