2

I'm currently working on a PowerShell script that includes the line

$process1 = Start-Process -FilePath ($exePath1) -PassThru -RedirectStandardError ($logPath1)

Which starts a long-running process and redirects the process' StandardError to a log file. My problem is that this also apparently interferes with ExitCode.

$process1.ExitCode

returns null after the process has exited. If I remove "RedirectStandardError ($logPath1)" then ExitCode returns the value my dummy-program is expected to return.

Should I be doing something different? I'm hoping to be able to start the process (redirecting the StandardError to the log file for diagnostics), wait a few seconds to make sure it doesn't crash, and retrieve the ErrorCode in the event that it does.

2
  • use $process1.WaitForExit() firstly before obtaining the $process1.ExitCode. Commented Jan 17, 2018 at 20:45
  • @t0mm13b: I'm using $process1.HasExited. The process in question is very vulnerable to configuration mistakes, so I want to start it up and make sure that it didn't crash out. Commented Jan 17, 2018 at 20:48

1 Answer 1

1

If you need to wait for the process and you're not running it as a different user:

$StartParams = @{
    FilePath = $exePath1
    RedirectStandardError = $logPath1
    PassThru = $True
    Wait = $True
}
$ReturnCode = (Start-Process @StartParams).ExitCode

Since it's long-running, here's an alternative method with PSJobs:

$Job = Start-Job -ScriptBlock {
    $StartParams = @{
        FilePath = $exePath1
        RedirectStandardError = $logPath1
        PassThru = $True
        Wait = $True
    }
    (Start-Process @StartParams).ExitCode
}
If ($Job.State -eq 'Completed')
{
    $ReturnCode = Receive-Job -Job $Job
}
Sign up to request clarification or add additional context in comments.

5 Comments

Upvote for the "Wait" idea, but this unfortunately doesn't work for me since the process in question is long-lived; I can't have my script hang waiting for it to possibly error out.
Kick off a child script as a job to do this bit then?
This partially works although the behavior is... odd. First off, you can't access variables from an outer scope, so I had to pass them in. Now it launches my console app, but no output is written to the console even on a Flush. I know that it's working, but nothing is shown. The weirdest thing though, is that $Job.State -eq 'Completed' actually returns true, even though -Wait is included. $Job.Error returns null. Any thoughts?
Oh that's right. Make sure you include the $using scope or you have to pass them in as arguments. @dornadigital As to your other concerns, does your console app write to stdout? Might want to try launching a new window. I assumed your exe was non-interactive considering how you're trying to run things.
Using $Job = Start-Job -ArgumentList $exePath1, $logPath1 -ScriptBlock { invoke-expression ("cmd /c start powershell -Command {(Start-Process -FilePath '" + $args[0] + "' -RedirectStandardError '" + $args[1] + "' -PassThru -Wait).ExitCode}") } launches everything as I'd expect, but I can't seem to access the ExitCode again. Receive-Job -Job $Job just returns null. Is this one of those speed and position problems, where you can know one but not the other?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.