If...Then...ElseIfIf...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 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
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 InformationThe 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 InformationThe 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 InformationThe 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
|