Reputation: 13
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
Upvotes: 0
Views: 2047
Reputation: 15247
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
Upvotes: 2