Error and exception handling – legacy exception handling

When you are developing your scripts, you may run into command-line tools that don't have a built-in exception handling function. This is common for third-party tools that only output the debugging information to the screen, and do not properly handle the exception. One of the most common methods to handle legacy command-based applications is by the use of variables. When you execute a command, you have the ability to check the output from that command. If the output is something other than what you expect, you have the ability to throw an exception.

For example, the netsh command has the ability to add firewall rules to your system. When you successfully add a firewall rule to a system, ...

Get PowerShell: Automating Administrative Tasks now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.