MicrosoftВ® Windows PowerShell(TM) Step By Step (Step By Step (Microsoft))
Now that we have mastered the process of looping through code, it is time to add some intelligence to our code. There are two statements we can use in our code to add intelligence. The first of the statements is the easiest to use and is the one that makes sense to most people: if … elseif … else. The reason it is so easy to use is that it is intuitive. We are used to saying things like: “If it is sunny this afternoon, then I will go surfing. In Windows PowerShell, the then clause is understood and not specifically used, but the syntax is still very intuitive.
The second decision-making statement is one that at first glance does not make sense: the switch statement. The switch statement is the Windows PowerShell equivalent to the select… case statement from Visual Basic days. When read as an if … elseif statement, switch makes sense. The advantage of the syntax is that it is much cleaner, involves less typing, and is easier to troubleshoot.
Using If … Elseif … Else
The if … elseif … else statement is used to add decision-making capabilities to the script. This can result in a great deal of flexibility in your script. By using the if statement you can evaluate a condition, and if the condition is true, you can take an action. When you add the else clause, you are able to evaluate a condition, but can now take two actions depending on how the condition evaluates. When you add elseif, you can evaluate a condition, but you now have a plethora of potential outcomes.
The
Value | Meaning |
---|---|
0 | x86 |
1 | MIPS |
2 | Alpha |
3 | PowerPC |
6 | Intel Itanium |
9 | x64 |
$wmi = get-wmiObject win32_processor if ($wmi.Architecture -eq 0) {"This is an x86 computer"} elseif($wmi.architecture -eq 1) {"This is an MIPS computer"} elseif($wmi.architecture -eq 2) {"This is an Alapha computer"} elseif($wmi.architecture -eq 3) {"This is an PowerPC computer"} elseif($wmi.architecture -eq 6) {"This is an IPF computer"} elseif($wmi.architecture -eq 9) {"This is an x64 computer"} else {$wmi.architecture + " is not a cpu type I am familiar with"} "Current clockspeed is : " + $wmi.CurrentClockSpeed + " MHZ" "Max clockspeed is : " + $wmi.MaxClockSpeed + " MHZ" "Current load percentage is: " + $wmi.LoadPercentage + " Percent" "The L2 cache size is: " + $wmi.L2CacheSize + " KB"
Using Switch
In other programming languages, switch would be called the select case statement. The switch statement is used to evaluate a condition against a series of potential matches. In this regard, it is essentially a streamlined if … elseif … else statement. When using the switch statement, the condition to be evaluated is contained inside parentheses. Each condition to be evaluated is then placed inside a curly bracket in the code block. This command is shown here:
$a=5;switch ($a) { 4{"four detected"} 5{"five detected"} }
In the
$wmi = get-wmiobject win32_computersystem "computer " + $wmi.name + " is: " switch ($wmi.domainrole) { 0 {"`t Stand alone workstation"} 1 {"`t Member workstation"} 2 {"`t Stand alone server"} 3 {"`t Member server"} 4 {"`t Back up domain controller"} 5 {"`t Primary domain controller"} default {"`t The role can not be determined"} }
Категории