Execute-Process failure not being caught in Try/Catch block

Hello, I am trying to handle an error in Execute-Process with my own try/catch blocks separate from the blocks try/catch blocks in the main script. This if for cleaning up mounted wim files if the installer fails. This is some example code below. The process I am running fails with exit code 127 but Execute-Process does not “trip” the try/catch logic when it fails and the catch block is completely skipped. Does anyone know what I may be missing or if this is working as intended?


$returncode = Execute-Process -path “my.exe” -parameters “–silent” -ExitOnProcessFailure $False -PassThru
$mainexitcode = $returncode.ExitCode
Show-DialogBox -text “$mainexitcode. I failed!”

Try this:

    try {
	    $return = Execute-Process -path "my.exe" -parameters "–silent" -ExitOnProcessFailure $False -PassThru
		$mainexitcode = $return.ExitCode
		If ((0,1641,3010) -contains $mainexitcode ) {
			Show-DialogBox -text "[$mainexitcode] Success!"
		} else {
			Throw "There was an Error. Here it is: [$($return.StdErr)] [$($return.StdOut)]"

    } catch {
	    #$mainexitcode = $returncode.ExitCode #No longer needed
	    Show-DialogBox -text "[$mainexitcode] I failed!" -TopMost $true -Icon Exclamation

FYI: if this SAP installer, there are more fail exitcodes than just 127. check the docs.