Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get return code from executing .Net program

Tags:

powershell

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?

like image 745
BigBytes Avatar asked Sep 08 '17 14:09

BigBytes


2 Answers

(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.

like image 156
Maximilian Burszley Avatar answered Sep 21 '22 09:09

Maximilian Burszley


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
like image 45
Bacon Bits Avatar answered Sep 24 '22 09:09

Bacon Bits