Practical PowerShell Uncategorized Try and Catch – Being Practical

Try and Catch – Being Practical

Practically speaking, Try and Catch pairs can perform many functions. They can simply be there for error handling, can be used to determine the next set of actions or even to execute and optional set of commands. In fact, we can use the pair to log different cmdlet outcomes if something fails or is not available to a particular PowerShell session. Let’s explore some of the ways we can use Try {} Catch {} in a practical manner.

Decision Making

This is an overly broad definition as it can cover many scenarios as well. However it is a very useful trick in PowerShell. Here are some examples:

Determine if a Module Loaded Properly

Some scripts may require cmdlets from multiple PowerShell modules, like Exchange and Active Directory for an example. On an Exchange Server we can determine if the AD PowerShell module was properly loaded like so:
[sourcecode language=”powershell”]
$ADModuleLoaded = $True
Try {
Import-Module ActiveDirectory -ErrorAction STOP
} Catch {
$ADModuleLoaded = $False
}
[/sourcecode]
In the above code, we set the $ADModuleLoaded variable to $True. If the module fails to load, then the same variable will be changed to $False. Later in the script we can then perform different actions based on the $ADModuleLoaded variable’s value. So, say the variable is true and thus the module loaded properly, we can run a series of AD related cmdlets. However if the $ADModuleLoaded variable is $False, then we could skip these cmdlets and record the problem in some sort of log file.

Determine To Proceed Further

Let’s say for an example we are checking an Exchange Server for the existence of Public Folders. If none exist, we skip trying to getting detailed stats on them. Whereas if we find the folders, we can instead check Public Folder statistics.
[sourcecode language=”powershell”]
$PFCheck = 0
Try {
$PFCheck = (Get-PublicFolder "\" -Recurse -ErrorAction STOP).Count
} Catch {
$Line = "No Public Folders can be found!" | Out-file $Destination -Append
}
If ($PFCheck -ne 0) {
Get-PublicFolder "\" -Recurse | Get-PublicFolderStatistics
}
[/sourcecode]
Above we can use PowerShell Try {} Catch to determine if there are Public Folders and thus the ability or possibility to check for Public Folders.

Catch Failures

Try and Catch is generally designed for this. We can use the cmdlet pair as a way to simply catch errors that may be generated by a cmdlet without putting them on the screen. Once caught, they can be logged to a file possibly like so:
[sourcecode language=”powershell”]
Try {
$Line = Get-DistributionGroup -ResultSize Unlimited | Where {$_.recipientTypeDetails -eq 'RoomList'}
} Catch {
$Line = 'Retrieving Room Lists Failed.' | Out-File $Destination -Append
$Line = "$Date,Error message – $_.Exception.Message" | Out-File $Destination -Append
}
[/sourcecode]
In the above code, we are looking for a special feature called Room Lists. If the cmdlet fails we log the fact that this failed as well as the error message generated ($_.Exception Message). Then when we review our scripts log file we can see what happened and possibly fix the issue.

Summary

As you can see, Try and Catch has some definite practical uses. The above list is certainly not comprehensive and more just illustrative of some uses of the cmdlet pair.

——————————————————————————————————————-
*** If you found this post useful, please comment or like the post.

Related Post