Practical PowerShell Uncategorized Conditional Variables

Conditional Variables

Making decisions in PowerShell. In this weeks PowerShell tip of the week we will review ways we can use variables to help drive scripts conditionally. This means that when a variable it set to a certain value then a subsection code its either run or skipped. Thus we can use variables conditionally to make this happen.

Example 1

For this sample code, a PowerShell query is made using CIMInstance and it fails, a good fallback is Get-WMIObject. We can use a combo of Try and Catch and a variable to store a result. The variable will be used to either trigger a WMI query (if the CIM query fails) or to ignore the WMI query if the CIM query worked as expected.

First section of code attempts to query a server via a CIM Instance. If the query failed (Inside the Try section) the code section in the Catch section will be run, and in this case it will set a $WMIQuery variable to $True:
[sourcecode language=”powershell”]
Try {
$MaximumSize = (Get-CimInstance -ComputerName $Server -Query "Select * from win32_PageFileSetting" -ErrorAction STOP| select-object MaximumSize).MaximumSize
} Catch {
$WMIQuery = $True
}
[/sourcecode]
Right after that the CIM query section, we can check the $WMIQuery variable to see it is true. If the variable is true (which it would be if the CIM query fails), then this code is executed:
[sourcecode language=”powershell”]
If ($WMIQuery) {

Try {
$MaximumSize = (Get-WMIObject -ComputerName $Server -Class win32_PageFileSetting -ErrorAction STOP | select-object MaximumSize).MaximumSize
} Catch {
$MaximumSize = 'Managed'
}
}
[/sourcecode]
Conditional variables can be used to create a determined flow in a script as well as be a part of error handling (Try {} Catch{}).

Example 2

In this next sample code block we will first set a variable ($Subscriptions) to $False. Then we will run a section of code to check to see if there are Edge Subscriptions (Edge Servers present in Exchange) and if the query fails, register this as no Edge subscriptions.
[sourcecode language=”powershell”]
$Subscriptions = $False Try { $EdgeSubscriptions = Get-EdgeSubscription -ErrorAction Stop | Select Name,Site,Domain,IsValid $Subscriptions = $True } Catch { $Line = 'No Edge Transport Servers or Subscriptions found.' | Out-File $Destination -Append }
[/sourcecode]
If the Get-EdgeSubscription succeeds and the $Subscriptions variable is changed from $False to $True. Once that variable set, we move on to another code section which will ONLY execute if the $Subscriptions variable is $True:
[sourcecode language=”powershell”]
If ($Subscriptions) { (Code to run) }
[/sourcecode]
This last section of code could be written a couple of ways:
[sourcecode language=”powershell”]
If ($Subscriptions) { }
[/sourcecode]
Or
[sourcecode language=”powershell”]
If ($Subscriptions -eq $True) {}
[/sourcecode]
Either way, if the $Subscriptions variable is $True, the code section in the brackets ( { } ) will execute. Now, we can also execute code if a variable false:
[sourcecode language=”powershell”]
If (!(Subscriptions) { }
[/sourcecode]
Or
[sourcecode language=”powershell”]
If ($Subscriptions -eq $False) {}
[/sourcecode]
** NOTE ** One thing to be careful of is that some cmdlets may not fail and only return a Null result, which would defeat the purpose of the Try{} Catch{} error handling. In cases like that, we may want to have an IF () check to see if a variable is Null instead. Still conditional because we are looking for a value to determine a course of action.

Example 3

In this last example, we can execute a code block based on what version of Exchange (or Windows) is running on a server. The first code section will determine the value for the $ExVersion variable:
[sourcecode language=”powershell”]
$ExVersion = $Null
If ($Version -like '*15.2*') {$ExVersion = '2019'}
If ($Version -like '*15.1*') {$ExVersion = '2016'}
If ($Version -like '*15.0*') {$ExVersion = '2013'}
If ($Version -like '*14.*') {$ExVersion = '2010'}
If ($Version -like '*8.*') {$ExVersion = '2007'}
[/sourcecode]
Once this variable is populated, we can then run a subset of code based on the value of the $ExVersion variable:
[sourcecode language=”powershell”]
If ($ExVersion -eq 2019) { }
If ($ExVersion -eq 2016) { }
If ($ExVersion -eq 2013) { }
[/sourcecode]
As we can see by the above examples, using a variables value to determine the direction of the script and what code to execute can be useful in some scenarios.

Related Post