Chapter 14. Errors and exceptions
This chapter covers
- Error handling
- Dealing with terminating errors
- Working with event logs
Progress, far from consisting in change, depends on retentiveness. Those who cannot remember the past are condemned to repeat it.
George Santayana, The Life of Reason
It’s always useful to keep in mind that PowerShell isn’t merely a shell or scripting language. Its primary purpose is to be an automation tool and perform critical management tasks on a server, such as send software updates, inspect log files, or provision user accounts. You need to be sure that either the task is completed properly or the reason for failure is appropriately recorded.
In this chapter, we’ll focus on how PowerShell reports, records, and manages error conditions. Handling of error conditions is one of the areas where PowerShell shines compared to other scripting tools. The support for diagnostic tracing and logging is practically unprecedented in traditional scripting languages. Unfortunately, these features don’t come entirely free—there are costs in terms of complexity and execution overhead that aren’t there in other environments. All these capabilities are a part of PowerShell as a management tool; Microsoft set a higher bar for PowerShell than has been set for most other language environments.