Previous Page
Next Page

If...Then...ElseIf

If...Then...ElseIf adds some flexibility to your ability to make decisions by using VBScript. If...Then enables you to evaluate one condition and take action based on that condition. By adding ElseIf to the mixture, you can make multiple decisions. You do this in the same way you did it using the If...Then command. You start out with an If...Then on the first line in the Worker information section, and when you are finished, you end the If...Then section with End If. If you need to make additional evaluations, add a line with ElseIf and the condition.

Just the Steps

To Use If...Then...ElseIf

1.
On a new line in the script, type If some condition Then.

2.
On the next line, enter the command you want to invoke.

3.
On the next line, type ElseIf and the new condition to check, and end the line with Then.

4.
On the next line, enter the command you want to invoke when the condition on the ElseIf line is true.

5.
Repeat steps 3 and 4 as required.

6.
On the next line, type End If.


You can have as many ElseIf lines as you need; however, if you use more than one or two, the script can get long and confusing. A better solution to avoid a long script is to convert to a Select Case type of structure, which is covered later in this chapter in the "Select Case" section.

Using the message box msgBox.vbs

1.
Open Notepad or the script editor of your choice.

2.
Define four variables that will hold the following: the title of the message box, the prompt for the message box, the button configuration, and the return code from the message box. The variables I used are strPrompt, strTitle, intBTN, intRTN. They are declared as follows:

Dim strPrompt
Dim strTitle
Dim intBTN
Dim intRTN

3.
Assign values to the first three variables. strPrompt is what you want to display to the user. The title will appear at the top of the message box. The value contained in strTitle will appear at the top of the message box. The variable intBTN is used to control the style of the buttons you want displayed.

strPrompt = "Do you want to run the script?"
strTitle = "MsgBOX DEMO"
intBTN = 3 '4 is yes/no 3 is yes/no/cancel

4.
Now write the code to display the message box. To do this, we will use intRTN to capture the return code from pressing the button. We will use each of our three message box variables as well and the msgBox function. The line of code looks like the following:

intRTN = MsgBox(strprompt,intBTN,strTitle)

5.
If you run the script right now, it will run and display a message box, but you are not evaluating the outcome. To do that, we will use If...Then...Else to evaluate the return code. It will look like the following:

If intRTN = vbYes Then
WScript.Echo "yes was pressed"
ElseIf intRTN = vbNo Then
WScript.Echo "no was pressed"
ElseIf intRTN = vbCancel Then
WScript.Echo "cancel was pressed"
Else
WScript.Echo intRTN & " was pressed"
End If

6.
Save the script as YourNameMsgBox.vbs and run it. It should tell you which button was pressed from the message box. You would then tie in the code to the appropriate button instead of just echoing the return values. Compare your code with msgBox.vbs in the folder \My Documents\Microsoft Press\VBScriptSBS\ch03 if required.

Let's examine a script that uses If...Then...ElseIf to detect the type of central processing unit (CPU) that is installed in a computer. Here is the CPUType.vbs script from the ch03 folder on the CD.

CPUType.vbs

Option Explicit
On Error Resume Next
Dim strComputer
Dim cpu
Dim wmiRoot
Dim objWMIService
Dim ObjProcessor
strComputer = "."
cpu = "win32_Processor='CPU0'"
wmiRoot = "winmgmts:\\" & strComputer & "\root\cimv2"
Set objWMIService = GetObject(wmiRoot)
Set objProcessor = objWMIService.Get(cpu)
If objProcessor.Architecture = 0 Then
  WScript.Echo "This is an x86 cpu."
ElseIf objProcessor.Architecture = 1 Then
  WScript.Echo "This is a MIPS cpu."
ElseIf objProcessor.Architecture = 2 Then
  WScript.Echo "This is an Alpha cpu."
ElseIf objProcessor.Architecture = 3 Then
  WScript.Echo "This is a PowerPC cpu."
ElseIf objProcessor.Architecture = 6 Then
  WScript.Echo "This is an ia64 cpu."
Else
  WScript.Echo "Cannot determine cpu type."
End If

Header Information

The Header information section contains the usual information (discussed in Chapters 1 and 2), as shown here:

Option Explicit
On Error Resume Next
Dim strComputer
Dim cpu
Dim wmiRoot
Dim objWMIService
Dim objProcessor

Option Explicit tells VBScript that you'll name all the variables used in the script by using the Dim command. On Error Resume Next turns on basic error handling. The strComputer variable holds the name of the computer from which we will perform the Windows Management Instrumentation (WMI) query. The cpu variable tells VBScript where in WMI we will go to read the information.

The wmiRoot variable enables you to perform a task you haven't performed before in previous scripts: split out the connection portion of WMI to make it easier to change and more readable. The variables objWMIService and objProcessor hold information that comes back from the Reference information section.

Reference Information

The Reference information section is the place where you assign values to the variables you named earlier in the script. The CPUType.vbs script contains these assignments:

strComputer = "."
cpu = "win32_Processor.deviceID='CPU0'"
wmiRoot = "winmgmts:\\" & strComputer & "\root\cimv2"

strComputer is equal to ".", which is a shorthand notation for the local computer that the script is currently executing on. With the cpu variable, you define the place in WMI that contains information about processors, which is win32_Processor. Because there can be more than one processor on a machine, you further limit your query to CPU0. It is necessary to use CPU0 instead of CPU1 because win32_Processor begins counting CPUs with 0, and although a computer always has a CPU0, it does not always have a CPU1. DeviceID is the key value for the WIN32_Processor WMI class. To connect to an individual instance of a processor, it is necessary to use the key value. The key of a WMI class can be discovered using wmisdk_book.chm from \My Documents\Microsoft Press\VBScriptSBS\resources, or by using the wbemTest.exe utility from a CMD prompt. In this script, you're only trying to determine the type of CPU running on the machine, so it isn't necessary to identify all CPUs on the machine.

Worker and Output Information

The first part of the script declared the variables to be used in the script, and the second part of the script assigned values to some of the variables. In the next section, you use those variables in an If...Then...ElseIf construction to make a decision about the type of CPU installed on the computer.

The Worker and Output information section of the CPUType.vbs script is listed here:

Set objWMIService = GetObject(wmiRoot)
Set objProcessor = objWMIService.Get(cpu)

If objProcessor.Architecture = 0 Then
  WScript.Echo "This is an x86 cpu."
ElseIf objProcessor.Architecture = 1 Then
  WScript.Echo "This is a MIPS cpu."
ElseIf objProcessor.Architecture = 2 Then
  WScript.Echo "This is an Alpha cpu."
ElseIf objProcessor.Architecture = 3 Then
  WScript.Echo "This is a PowerPC cpu."
ElseIf objProcessor.Architecture = 6 Then
  WScript.Echo "This is an ia64 cpu."
Else
  WScript.Echo "Cannot determine cpu type."
End If

To write a script like this, you need to know how win32_Processor hands back information so that you can determine what a 0, 1, 2, 3, or 6 means. By detailing that information in an If...Then...ElseIf construct, you can translate the data into useful information.

The first two lines listed in the preceding script work just like a normal If...Then statement. The line begins with If and ends with Then. In the middle of the If...Then language is the statement you want to evaluate. If objProcessor returns a zero when asked about the architecture, you know the CPU is an x86, and you use WScript.Echo to print out that data.

If, on the other hand, objProcessor returns a one, you know that the CPU type is a millions of instructions per second (MIPS). By adding into the ElseIf statements the results of your research into return codes for WMI CPU types, you enable the script to handle the work of finding out what kind of CPU your servers are running. After you've used all the ElseIf statements required to parse all the possible return codes, you add one more line to cover the potential of an unexplained code, and you use Else for that purpose.

Combine msgBox and CPU information

1.
Open Notepad or the script editor of your choice.

2.
Open the msgBox.vbs script and save it as YourNamePromptCPU.vbs.

3.
At the very bottom of the newly renamed msgBox.vbs script, type Sub subCPU, as seen below:

Sub subCPU

4.
Open the CPUType.vbs script and copy the entire script to the clipboard.

5.
Paste the entire CPUType.vbs script under the words Sub subCPU.

6.
The first and second lines (from the CPUType.vbs script) that are pasted below the words Sub subCPU are not required in our subroutine. The two lines can be deleted. They are listed below:

Option Explicit
On Error Resume Next

7.
Go to the bottom of the script you pasted under the words Sub subCPU and type End sub. We have now moved the CPU-type script into a subroutine. We will only enter this subroutine if the user presses the Yes button.

8.
Under the code that evaluates the vbYes intrinsic constant, we want to add the line to call the subroutine. To do this, we simply type the name of the subroutine. That name is subCPU. The code to launch the script is seen below. Notice the only new code here is the word subCPU, everything else was already in the msgBox script.

If intRTN = vbYes Then
WScript.Echo "yes was pressed"
subCPU

9.
For every other button selection, we want to end the script. The command to do that is WScript.quit. We will need to type this command in three different places, as seen below:

ElseIf intRTN = vbNo Then
WScript.Echo "no was pressed"
WScript.quit
ElseIf intRTN = vbCancel Then
WScript.Echo "cancel was pressed"
WScript.quit
Else
WScript.Echo intRTN & " was pressed"
WScript.quit
End If

10.
Save and run the script. Press the Yes button, and the results from CPUType should be displayed. Run the script two more times: Press No, and then press Cancel. On each successive running, the script should exit instead of running the script. If these are not your results, compare the script with the PromptCPU.vbs script in the folder \My Documents\Microsoft Press\VBScriptSBS\ch03.

Quick Check

Q.

How many ElseIf lines can be used in a script?

A.

As many ElseIf lines as are needed.

Q.

If more than one or two ElseIf lines are necessary, is there another construct that would be easier to use?

A.

Yes. Use a Select Case type of structure.

Q.

What is the effect of using strComputer = "." in a script?

A.

The code strComputer is shorthand that means the local computer the script is executing on. It is used with WMI.



Previous Page
Next Page