Search code examples
powershellsalt-project

Retrieving actual return code of a Powershell cmdlet


I am trying to get a return code from a Powershell cmdlet. This return code will be passed to a Saltstack cmd.run state (success_retcodes parameter)

My cmdlet that should produce an error:Get-PSSessionConfiguration test

This will produce an error because test session does not exist. Running $? right after always produces True whether the above cmdlet fails or not.

The purpose of this exercise is purposely not fail one of my states in Saltstack via the aforementioned parameter which I believe takes a numeral value. Any suggestions would be appreciated.


Solution

  • Why not just use the good ol' try/catch block?

    Try {
        Get-PSSessionConfiguration test -ErrorAction stop
        return $true
    } Catch {
        return $false
    }