From the course: PowerShell: Automating Administration

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Error handling with try, catch, and finally

Error handling with try, catch, and finally - Server Tutorial

From the course: PowerShell: Automating Administration

Start my 1-month free trial

Error handling with try, catch, and finally

- [Instructor] There are a few different ways to write planned response into your scripts. One way is to set a global variable. The variable ErrorActionPreference. The variable ErrorActionPreference will set the default response to all non-terminating exceptions to one of the four settings. This may not be your best option, because you may not want the same action for all errors. For example, if you have a script that uses WMI to pull information from every workstation in your network, you may be tired of error messages that tell you that the remote RPC server is not available. You may be tempted to set the ErrorActionPreference global variable to SilentlyContinue. That will have the effect of suppressing those error messages and all other messages as well, including ones that you may want to know about. A second approach is to set the error exception at the individual commandlet level. If I was to run GetContent, and let's go ahead and specify a file that doesn't exist, let's say in…

Contents