Reputation: 339
Does anyone know how to get the return code from a .NET executable? I've written a program that has a static main method that returns an int and I can't seem to get this number when I run it from powershell. This is what I currently have:
&$executable $params
exit $LASTEXITCODE
where $executable
is the path to the executable and $params
are the parameters passed to the executable.
However, $LASTEXITCODE
is always 0. The program writes to the console via a Log4Net console appender so the above pipes the output to the console in PowerShell.
Can anyone help?
Upvotes: 6
Views: 2415
Reputation: 19644
(Start-Process -FilePath 'exe' -ArgumentList @() -PassThru -Wait).ExitCode
This will grab your exit code after execution completes. You could even assign it to a variable and access the process members if you want.
Upvotes: 4
Reputation: 32145
Sounds to me like your executable doesn't return an exit code (i.e., Environment.Exit()
) but instead outputs a result code (e.g., Console.Write()
).
Try something like:
$ReturnValue = &$executable $params
Exit $ReturnValue
Upvotes: 2