O'Reilly logo

Learn PowerShell Toolmaking in a Month of Lunches by Don Jones and Jeffrey Hicks

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

10.5. Error Handling v2+: Try...Catch...Finally

Introduced in PowerShell v2, this is the real way to do error handling. All the cool kids are doing it, because it’s easier and much more like the error handling found in traditional programming languages. It’s called Try...Catch...Finally, and we’re going to slap it into our Get-SystemInfo function to show you how it works. Check out this listing.

Listing 10.1. Adding Try...Catch to our function
function Get-SystemInfo { <# .SYNOPSIS Retrieves key system version and model information from one to ten computers. .DESCRIPTION Get-SystemInfo uses Windows Management Instrumentation (WMI) to retrieve information from one or more computers. Specify computers by name or by IP address. .PARAMETER ComputerName ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required