This document discusses error handling in PowerShell. It covers exceptions, the $ErrorActionPreference variable and -ErrorAction parameter to control error behavior, the $error predefined variable to access errors, the -Errorvariable parameter to store errors in a variable, trap constructs to handle errors, and checking for errors from non-PowerShell commands using $? and $LastExitCode. The document provides details on how to configure error behavior using these methods and best practices for error handling in scripts.
5. $ErrorActionPreference and ErrorAction
1. Inquire
When error occur it ask user for conformation
We cannot use it in script as it required user interaction which change the meaning of
automation is it
We can use it to check command functionally but not prefer
2. Continue
This is a default settings of built-in variable
When error occur it will not ask any thing to user display the error and keep going
3. Silently Continue
Does not display any error and keep going
I wish my girl friend should be like this
4. Stop
When error occur it terminate the script
Also terminate non-terminating script too
Using try and catch is good option
6. Predefine Variable ($error)
1. Predefined variable
2. Data type : Array
3.Default Array Size is 256
Note: To know how many Predefine Variable (Get-Variable)
7. -Errorvariable (-EV)
1. One of the Common Parameters
2. parameter accepts the name of variable
3. If command generate an error its place into that variable
4. Best way to use with silentlycontinue option of Error
Action
Common parameters
Get-help about_commonparameters |more
8. General Trap /Trap Constructs
1. Introduced in Powershell v1
2. When ever trappable exception occurs Powershell jump back
in your script and execute TRAP CONSTRUCTS
3. It always placed starting of script
9. Non-Powershell Commands
$? -> just returns True or False depending on
whether the last command (cmdlet or native)
exited without error or not
$LastExitCode