Making Decisions


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 image from book GetCPUinfo.ps1 script illustrates using if elseif else. The script will retrieve information about the CPU on a computer by using the Get-WmiObject cmdlet. The $wmi variable is used to hold the processor object that is returned. The architecture property of the processor management object only reports a coded value, as seen in Table 4-7. To make the script more readable, an if elseif else statement is used to translate each value with the specific type of processor.

Table 4-7: WIN32_Processor Processor Values
Open table as spreadsheet

Value

Meaning

0

x86

1

MIPS

2

Alpha

3

PowerPC

6

Intel Itanium

9

x64

image from book GetCPUinfo.ps1

 $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 image from book DisplayComputerRoles.ps1 script shown here, the script begins by using the $wmi variable to hold the object that is returned by using the Get-WmiObject cmdlet. The domainrole property of the WIN32_ComputerSystem class is returned as a coded value. To produce an output that is more readable, the switch statement is used to match the value of the domainrole property to the appropriate text value.

image from book DisplayComputerRoles.ps1

 $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"}    }




Microsoft Press - Microsoft Windows PowerShell Step by Step
MicrosoftВ® Windows PowerShell(TM) Step By Step (Step By Step (Microsoft))
ISBN: 0735623953
EAN: 2147483647
Year: 2007
Pages: 128
Authors: Ed Wilson

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net