Previous Page
Next Page

Select Case

When I see a Select Case statement in a script written in the VBScript language, my respect for the script writer goes up at least one notch. Most beginning script writers can figure out the If...Then statement, and some even get the If...Then...Else construction down. However, few master the Select Case construction. This is really a shame, because Select Case is both elegant and powerful. Luckily for you, I love Select Case and you will be masters of this construction by the end of this chapter!

Just the Steps

To use Select Case

1.
On a new line in the script, type Select Case and a variable to evaluate.

2.
On the second line, type Case 0.

3.
On the third line, assign a value to a variable.

4.
On the next line, type Case 1.

5.
On a new line, assign a value to a variable.

6.
On the next line, type End Select.


In the following script, you again use WMI to obtain information about your computer. This script is used to tell us the role that the computer plays on a network (that is, whether it's a domain controller, a member server, or a member workstation). You need to use Select Case to parse the results that come back from WMI, because the answer is returned in the form of 0, 1, 2, 3, 4, or 5. Six options would be too messy for an If...Then...ElseIf construction. The text of ComputerRoles.vbs is listed here:

ComputerRoles.vbs

Option Explicit
'On Error Resume Next
Dim strComputer
Dim wmiRoot
Dim wmiQuery
Dim objWMIService
Dim colItems
Dim objItem

strComputer = "."
wmiRoot = "winmgmts:\\" & strComputer & "\root\cimv2"
wmiQuery = "Select DomainRole from Win32_ComputerSystem"

Set objWMIService = GetObject(wmiRoot)
Set colItems = objWMIService.ExecQuery _
  (wmiQuery)
For Each objItem in colItems
  WScript.Echo funComputerRole(objItem.DomainRole)
Next

Function funComputerRole(intIN)
 Select Case intIN
  Case 0
    funComputerRole = "Standalone Workstation"
  Case 1
    funComputerRole = "Member Workstation"
  Case 2
    funComputerRole = "Standalone Server"
  Case 3
    funComputerRole = "Member Server"
  Case 4
    funComputerRole = "Backup Domain Controller"
  Case 5
    funComputerRole = "Primary Domain Controller"
  Case Else
  funComputerRole = "Look this one up in SDK"
 End Select
End Function

Header Information

The Header information section of ComputerRoles.vbs is listed in the next bit of code. Notice that you start with the Option Explicit and On Error Resume Next statements, which are explained earlier in this chapter and in detail in Chapter 1, "Starting from Scratch." Next, you declare six variables. wmiQuery is, however, a different variable. You'll use it in the Reference information section, where you'll assign a WMI query string to it. By declaring a variable and listing it separately, you can change the WMI query without having to rewrite the entire script.

objWMIService is used to hold your connection to WMI, and the variable colItems holds a collection of items that comes back from the WMI query. objItem is used to obtain an individual item from the collection. This was discussed in Chapter 2. The complete Header information section is listed below:

Option Explicit
On Error Resume Next
Dim strComputer
Dim wmiRoot
Dim wmiQuery
Dim objWMIService
Dim colItems
Dim objItem

Reference Information

The Reference information section assigns values to many of the variables named in the Header information part of ComputerRoles.vbs. The Reference information section of the script is listed here:

strComputer = "."
wmiRoot = "winmgmts:\\" & strComputer & "\root\cimv2"
wmiQuery = "Select DomainRole from Win32_ComputerSystem"

Two variables are unique to this script, the first of which is wmiQuery. In the CollectionOfDrives.vbs script discussed in Chapter 2, you embedded the WMI query in the GetObject command, which makes for a long line. By bringing the query out of the GetObject command and assigning it to the wmiQuery variable, you make the script easier to read and modify in the future. Next, you use the colItems variable and assign it to hold the object that is returned when you actually execute the WMI query.

Quick Check

Q.

How is Select Case implemented?

A.

Select Case begins with the Select Case command and a variable to be evaluated. However, it is often preceded by a For Each statement.

Q.

How does Select Case work?

A.

Select Case evaluates the test expression following the Select Case statement. If the result from this matches a value in any of the Case statements, it executes the code following that Case statement.

Q.

What is the advantage of assigning a WMI query to a variable?

A.

It provides the ability to easily use the script to query additional information from WMI.


Worker and Output Information

As mentioned earlier, WMI often returns information in the form of a collection (we talked about this in Chapter 2), and to work your way through a collection, you need to use the For Each...Next command structure to pull out specific information. In the Worker information section of ComputerRoles.vbs, you begin with making a connection into WMI. We do this by using GetObject to obtain a hook into WMI. Once we have made the connection into WMI, we then execute a WMI query and assign the resulting collection of items to a variable called colItems. We then use For Each...Next to pull one instance of an item from the collection so we can examine it. This is illustrated in code below. The interesting thing is the way we moved the Select Case statement from the middle of the script into a function called funComputerRole.

Set objWMIService = GetObject(wmiRoot)
Set colItems = objWMIService.ExecQuery _
  (wmiQuery)
For Each objItem in colItems
  WScript.Echo funComputerRole(objItem.DomainRole)
Next

funComputerRole function

To simplify the reading of the script, and to make it easier to maintain the script, we move the Select Case structure into a function we include at the bottom of the script. This moves the decision matrix out of the middle of the Worker section. It vastly simplifies the code (once, of course, you understand how a function works). In Figure 3-1, you can see that when we call the function, we use the name of the function. In parentheses, we include the parameter we wish to supply to the function. In the ComputerRoles.vbs script, we are retrieving a numeric value that corresponds to the role the computer plays in the network. If you look up the WIN32_computerSystem class in the WMI Platform SDK (\My Documents\Microsoft Press\VBScriptSBS\Resources\WMI_SDKBook.chm), you will see an article that lists all the valuable properties this class can supply. I have copied the values for domain role into Table 3-1. Based on the chart that translates the values for computer role, I created the funComputerRole function.

Figure 3-1. Assign value to name of the function


Table 3-1. WMI Domain Roles from Win32_ComputerSystem

Value

Meaning

0

Standalone workstation

1

Member workstation

2

Standalone server

3

Member server

4

Backup domain controller

5

Primary domain controller


In Figure 3-1, you see how the numeric value of the computer role is passed to the funComputerRole function. Once the number is inside the function, it is stored in the variable intIN. The Select Case statement evaluates the value of intIN and assigns the appropriate string to the name of the function itself. As you can see in Figure 3-1, the value that is assigned to the name of the function is passed back to the line of code that called the function in the first place. The funComputerRole function is listed below.

Function funComputerRole(intIN)
 Select Case intIN
  Case 0
    funComputerRole = "Standalone Workstation"
  Case 1
    funComputerRole = "Member Workstation"
  Case 2
    funComputerRole = "Standalone Server"
  Case 3
    funComputerRole = "Member Server"
  Case 4
    funComputerRole = "Backup Domain Controller"
  Case 5
    funComputerRole = "Primary Domain Controller"
  Case Else
  funComputerRole = "Look this one up in SDK"
 End Select
End Function

To find out how the DomainRole field is structured, you need to reference the Platform SDK for Microsoft Windows Server 2003. You will also be able to find other properties you can use to expand upon the ComputerRoles.vbs script. The value descriptions for domain roles are shown in Table 3-1.

The first line of the Select Case statement contains the test expressionthe number representing the role of the computer on the network. Each successive Case statement is used to evaluate the test expression, and to identify the correct computer role. The first of these statements is seen here:

Case 0
  strComputerRole = "Standalone Workstation"

The strComputerRole variable will be assigned the phrase "Standalone Workstation" if the text expression (intIN) is equal to 0. You will then use strComputerRole to echo out the role of the computer in the domain when we exit the Select Case construction.

You end the Select Case construction with End Select, similarly to the way you ended the If...Then statement with End If. After you use End Select, you use the WScript.Echo command to send the value of strComputerRole out to the user. Remember that the entire purpose of the Select Case construction in ComputerRoles.vbs is to find and assign the DomainRole value to the strComputerRole variable. After this is accomplished, you use the Next command to feed back into the For Each loop used to begin the script.


Previous Page
Next Page