Try / Catch / Finally
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #Try block try { $ErrorCount = 0 #Creating an error Get-NotARealCommand } catch { $ErrorCount = $ErrorCount + 1 #Writing the error to the console including the line number it was on Write-Host "Error Message: $($_.Exception.Message) Error on line $($_.InvocationInfo.ScriptLineNumber)" Write-Host "Trying command #2" Get-TimeZone | Format-Table id } finally { #At the end of the catch do one last thing "finally" Write-Host "Error count is $ErrorCount" } |
1 2 3 4 5 6 7 8 9 | Error Message: The term 'Get-NotARealCommand' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. Error on line 3 Trying command #2 Id -- Pacific Standard Time Error count is 1 |
Error Stream
1 | $Error [0] |
1 2 3 4 5 6 | Get-NotARealCommand: Line | 3 | Get-NotARealCommand | ~~~~~~~~~~~~~~~~~~~ | The term 'Get-NotARealCommand' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. |
1 | $Error [0] | fl * -Force |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | PSMessageDetails : Exception : System.Management.Automation.CommandNotFoundException: The term 'Get-NotARealCommand' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception) at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame) at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame) at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame) TargetObject : Get-NotARealCommand CategoryInfo : ObjectNotFound: (Get-NotARealCommand:String) [], CommandNotFoundException FullyQualifiedErrorId : CommandNotFoundException ErrorDetails : InvocationInfo : System.Management.Automation.InvocationInfo ScriptStackTrace : at <ScriptBlock>, <No file>: line 3 PipelineIterationInfo : {} |
Error Action
1 2 3 | $ErrorActionPreference = 'Stop' Get-NotARealCommand Write-Host "This text is after the error" |
1 2 3 4 5 | Line | 2 | Get-NotARealCommand | ~~~~~~~~~~~~~~~~~~~ | The term 'Get-NotARealCommand' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is | correct and try again. |
1 2 3 | $ErrorActionPreference = 'Continue' Get-NotARealCommand Write-Host "This text is after the error" |
1 2 3 4 5 6 7 | Line | 2 | Get-NotARealCommand | ~~~~~~~~~~~~~~~~~~~ | The term 'Get-NotARealCommand' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is | correct and try again. This text is after the error |
1 2 3 | $ErrorActionPreference = 'SilentlyContinue' Get-NotARealCommand Write-Host "This text is after the error" |
1 | This text is after the error |