AndyGilly Posted February 26, 2021 Posted February 26, 2021 We are trying to put some error handling into our PowerShell scripts that are making packages in ITOM. When the script is processing it does not seem to be able to step from a 'try' statement into a 'catch' The try is processed successfully if everything is valid but if its not then falling into the catch does not happen. The Powershell script itself seems to run OK in Powershell ISE would appreciate any thoughts or guidance thanks Andy
Steve G Posted February 26, 2021 Posted February 26, 2021 Hi @AndyGilly, This is posssibly due to the default ErrorAction for the cmdlet in question not being Stop. I've seen examples where the error is output to the CLI and the script continues as normal, even when in a try-catch. If the cmdlet you are using supports it, then set the -ErrorAction Stop argument (either directly in the command line, or by splatting the param(s) in a hash table, example below). This will ensure the error can be caught and handled gracefully. try { $Params = @{ Name = $Name ErrorAction = "Stop" WarningVariable = "warnings" WarningAction = "SilentlyContinue" } Start-VM @Params } catch { Write-Output "{{SISJobOutputParameterStart:errors}}$($_.Exception.Message){{SISJobOutputParameterEnd}}" Write-Output "{{SISJobOutputParameterStart:outcome}}FAIL{{SISJobOutputParameterEnd}}" Exit 0 } Cheers, Steve 1
AndyGilly Posted February 26, 2021 Author Posted February 26, 2021 Thanks for the quick reply @Steve G i will have a chat with the dev
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now