-
Notifications
You must be signed in to change notification settings - Fork 7.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Process objects created with Start-Process -NoNewWindow and/or -RedirectStandard* never report their exit code on Windows. #5421
Comments
Dug this up... Seems as though it's not so much waiting for it to end as it is explicitly querying for the process handle while it's still running is what allows the exit code to be captured. Ideally, |
Great find, @vexx32 - that sounds like a great workaround. A little more background info to flesh out your statement, from a linked answer:
I wonder if there was ever a good reason for this behavior, or whether we should also file a CoreFx bug report. |
It probably can't hurt, honestly. But meantime, we can temp-fix this fairly quickly, it would appear. 😄 I'm not familiar with the internals of that object, but I wouldn't be surprised if there was some weirdness where it accesses the ExitCode via the handle reference somehow, so if it isn't retrieved before the process exits, it can't get at it, or something. Should still be fixed either way, heh. |
Any updates on this? This really should be addressed. |
Any update? This is a rather impactful productivity blocker because you can't tell if process succeeded or failed. Makes start-process pretty much useless. |
Per #20716 (comment), the workaround (only applicable if |
A fix may land in v7.4.1 (the PR mentions only the related #20400 issue): |
This issue has not had any activity in 6 months, if there is no further activity in 7 days, the issue will be closed automatically. Activity in this case refers only to comments on the issue. If the issue is closed and you are the author, you can re-open the issue using the button below. Please add more information to be considered during retriage. If you are not the author but the issue is impacting you after it has been closed, please submit a new issue with updated details and a link to this issue and the original. |
It looks like the problem has indeed been fixed, as verified via the original repro steps, in the current version, v7.4.2. |
Note:
-Wait
in addition to-PassThru
as opposed to a separateWait-Process
call.-Wait
is used.Workaround:
Per #20716 (comment), the workaround is to cache the process handle before calling
Wait-Process
/.WaitForExit()
:Steps to reproduce
On Windows:
Note that the problem only occurs when
-NoNewWindows
and /or any of the-RedirectStandard*
parameters are present.Inserting a
$p.WaitForExit()
call after theWait-Process
call, as suggested in the docs to ensure that.ExitCode
has a value (which should be the equivalent ofWait-Process
), doesn't help.Expected behavior
$ps.ExitCode
should contain0
, given that thecmd
command reports exit code0
.Actual behavior
Note how
$ps.ExitCode
is unexpectedly stringified to the empty string.Also, even though
Get-Member
indicates that the property's date type is[int]
,$null -eq $ps.ExitCode
is$true
.This suggests that an exception is occurring behind the scenes, which PowerShell quietly ignores.
An exception when accessing
.ExitCode
should only occur if the process hasn't exited yet, however, which is at odds with having usedWait-Process
and$p.HasExited
indicating$true
.Environment data
The text was updated successfully, but these errors were encountered: