Chapter 10. Error handling

The Get-SystemInfo function we wrote earlier was designed from the outset to log the names of computers it failed to reach. In this chapter, we’ll make that happen, through a set of techniques collectively known as error handling.

10.1. It’s all about the action

Whenever a PowerShell command—be it a native cmdlet or a function you write—encounters a non-terminating error, it asks PowerShell what to do. PowerShell looks at a built-in variable, $ErrorActionPreference, to see what it should do.

Before we dive into that, let’s talk about this non-terminating error thing. It’s any error that presents a problem, but one from which the command can recover and continue. “Hey, this computer was unreachable, but if you want, ...

Get Learn PowerShell Toolmaking in a Month of Lunches now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.