If...Then...ElseIf

上一篇 / 下一篇  2010-03-24 19:59:14 / 个人分类:VBScript

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.

 

 

Example:Get the CPU informaiton

Option Explicit
'On Error Resume Next
Dim strPrompt
Dim strTitle
Dim intBTN
Dim intRTN
Dim FSO
strPrompt = "Do you want to run the script?"
strTitle = "MSGBOX DEMO"
intBTN = 3
intRTN = MsgBox (strPrompt,intBTN,strTitle)
If intRTN = vbYes Then
WScript.Echo "yes was pressed"
subCPU
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

Sub subCPU()
Dim strComputer
Dim cpu
Dim wmiRoot
Dim objWMIService
Dim ObjProcessor
strComputer = "."
cpu = "win32_Processor='CPU0'"
wmiRoot = "winmgmts:\\" & strComputer & "\root\cimv2"
Set bjWMIService = GetObject(wmiRoot)
Set bjProcessor = 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
End Sub

 

change the If...Then...ElseIf to Select Case...Case...End Select

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 bjWMIService = 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

 

 

 

From

Microsoft@ VBScript. Step by Step
By Ed Wilson

 

 


TAG:

 

评分:0

我来说两句

Open Toolbar