3

I'm trying to get the exit code of my wpf application called in a Powershell script.

My main in WPF :

[STAThread]
public static int Main(string[] args)
{
    if (args != null && args.Length > 0)
    {
        NB_ERRORS = AutomaticTests.Program.Main(args);
        return NB_ERRORS;
        //Application.Current.Shutdown(NB_ERRORS);
    }
    else
    {
        App app = new App();
        app.Run(new MainWindow());

        //Application.Current.Shutdown(NB_ERRORS);
        return NB_ERRORS;
    }
}

And in powershell I call it like this :

& $PathToExeTests 0 $LogTestsStagging
$nbFailed = $LASTEXITCODE

But it always contains 0.

I've tried to manually set the Environment.ExitCode, to shutdown the application with the code, to override OnExit like this :

protected override void OnExit(ExitEventArgs e)
{
    e.ApplicationExitCode = AutomaticTests.GUI.Program.NB_ERRORS;
    base.OnExit(e);
}

But I always have 0 in the LastExitCode.

Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
user2088807
  • 1,378
  • 2
  • 25
  • 47
  • the exit code by default is 0...https://msdn.microsoft.com/en-us/library/system.environment.exitcode(v=vs.110).aspx – Bernard Walters Mar 20 '17 at 13:57
  • That's why I'm trying to set a custom one to be able to know how many errors I got in my WPF application. – user2088807 Mar 20 '17 at 14:01
  • 1
    I don`t know in power shell but maybe there is something similar to command line. In command line you have to run the application with "start /wait [Application.exe]" and only then you can use %errorlevel% to get the result back. Maybe power shell has something similar – shahar eldad Mar 20 '17 at 14:04
  • Does this helpl? http://stackoverflow.com/questions/7772015/wpf-application-exit-code?rq=1 – Matt Mar 20 '17 at 14:28

1 Answers1

1

By default, when you start a GUI app, PowerShell (just like cmd.exe) will not wait for the app to exit; it'll just tell Windows to start loading the app, and then continue running the script.

There are a couple of ways to wait for a GUI app to exit.

Option 1: Start the app using Start-Process, and then pass the resulting Process object to Wait-Process. This can be easily written as a pipeline:

Start-Process $PathToExeTests -ArgumentList @(0, $LogTestsStaging) | Wait-Process

Option 2: If you do something with the app's standard output (assign it into a variable, or pipe it into another command), then PowerShell will automatically wait for the process to exit.

& $PathToExeTests 0 $LogTestsStaging | Out-Null

Option 1 is probably going to be a lot more readable if someone else is ever going to maintain your code, but occasionally you'll see option 2 as well.

Joe White
  • 94,807
  • 60
  • 220
  • 330
  • This doesn't mention exit codes at all but for googlers: for a GUI process, you must `-Wait -PassThru` - this ensures that the process object will have its `$p.ExitCode` set correctly. More details [here](https://stackoverflow.com/a/16018287/33080) and [here](https://stackoverflow.com/a/39497158/33080). – Roman Starkov Dec 01 '21 at 10:20