witjet
witjet

Reputation: 13

LASTEXITCODE in powershell is alway 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 :

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

Answers (1)

Cid
Cid

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

Related Questions