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?
(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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With