Jump to content

ITOM Powershell Packages


AndyGilly

Recommended Posts

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

 

Link to comment
Share on other sites

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

  • Like 1
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...