2

I have a powershell script that defines $ErrorActionPreference = "Stop"
But i also have a start-process call that target a process that returns a non-standard exit code on success (1 instead of 0). As a result of this, the script is failing even when the start-process is ok. I tried to append the -ErrorAction "Continue" parameter in the start-process call but it didn't solve the problem.

The problematic line looks like this:

$ErrorActionPreference = "Stop"
...
start-process "binary.exe" -Wait -ErrorAction "Continue"
if ($LastExitCode -ne 1)
{
   echo "The executable failed to execute properly."
   exit -1
}
...

How could I prevent start-process from making the whole script fail.

1
  • Have you tried setting $ErrorActionPreference = "silentlycontinue" on the previous line, and then $ErrorActionPreference = "Stop" on the following line? Commented Jun 12, 2015 at 15:08

1 Answer 1

3

Start-Process doesn't update $LASTEXITCODE. Run Start-Process with the -PassThru parameter to get the process object, and evaluate that object's ExitCode property:

$ErrorActionPreference = "Stop"
...
$p = Start-Process "binary.exe" -Wait -PassThru
if ($p.ExitCode -ne 1) {
  echo "The executable failed to execute properly."
  exit -1
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.