0

I am not a great powershell expert and I stumble on a problem that is undoubtedly trivial

When I run the following command in CMD (SAP related but it's not important)

.\sapcontrol.exe -nr 00 -prot PIPE -function GetProcessList

I get an exit code which may have different values depending on the state of the processes :

  • 0 Last webmethod call successful

  • 1 Last webmethod call failed, invalid parameter

  • 2 StartWait, StopWait, WaitforStarted, WaitforStopped, RestartServiceWait timed out

  • 3 GetProcessList succeeded, all processes running correctly

  • 4 GetProcessList succeeded, all processes stopped

In powershell when i use

Start-Process or & cmd.exe / c my $LASTEXITCODE or $? always returns 0 (probably to tell me that the command has been executed ... but that's not what I want ...)

how to get the equivalent of EXITCODES in powershell ?

thanks in advance

2
  • 2
    For $LASTEXITCODE to be populated, you need to invoke the executable directly, eg. & .\sapcontrol.exe .... If you're using Start-Process, you'll have to check the ExitCode property on the Process object returned by Start-Process Commented Sep 1, 2021 at 10:02
  • why do you use Start-Process instead of running the exe file directly? Commented Sep 1, 2021 at 10:02

1 Answer 1

2

With Start-Process you can pass the argument -PassThru to get an object containing the process. Then, you can use the exit code property inside that object using $YourProcess.ExitCode :

$oProcess = Start-Process -Path $executablePath -ArgumentList $executableArguments -Wait -PassThru
$exitcode = $oProcess.ExitCode

More infos in documentation

Sign up to request clarification or add additional context in comments.

2 Comments

Start-Process here is not necessary, because the OP calls the exe from cmd directly instead of start sapcontrol.exe in cmd
@phuclv of course, it's just one of the ways to do it

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.