PowerShell Ezine, Logon Scripts

Guy's Scripting Ezine 46 - VBScript MsgBox

VBScript MsgBox Examples

 ♣

This Week's Secret

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) IP Tracker

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

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.

Example 1 Simple Message Box With One Button - OK.

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 VBScript MsgBox () Function

  1. Copy and paste the script below into notepad.
  2. Save the file with .vbs extension e.g. MessageBox.vbs.
  3. Double click and examine the message box.

' MessageBox.vbs
' MsgBox VBScript to generate an input 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.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11

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.

Download a free trial of Solarwinds' Network Performance Monitor

Example 2 Message Box with 3 Buttons, Yes, No and Cancel

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.

' MessageBox3.vbs
' VBScript Msgbox 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")

Summary MsgBox

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

VBScripts  • WMI  • Ezines  • Logon Scripts  • Tool Kit  • SLA Monitor  • Ezine 26 Msg Box

Ezine 41 VBS Select case  • Ezine 46 MsgBox  • Free Response Time Tool

Ezine 55 VBS Select case  • Ezine 61 Objects methods   • Ezine 70 GetEx Split


Custom Search

Site Home

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

Fortunately, SolarWinds have created the Free WMI Monitor so that you can actually see and understand these gems of operating system information.  Take the guess work out of which WMI counters to use for scripts.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2014 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: