-
Notifications
You must be signed in to change notification settings - Fork 7.1k
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
Start-Process does not populate the process object's exit code with NoNewWindow switch #20400
Comments
Although the symptoms have changed a bit, the root cause is likely the same as in: That is, it seems that an exception occurs behind the scenes due to the process handle not being available (for reasons unknown to me), which PowerShell quietly swallows, causing This now seems to always happen with That is, both In Windows PowerShell (and seemingly also earlier versions of PS Core), the The problem does NOT occur when # NOTE: You may also have to set the working dir. to PowerShell's.
$p = [System.Diagnostics.Process]::Start('pwsh', (Convert-Path 'script.ps1'))
$p.WaitForExit()
"Exit code: [$($p.ExitCode)]" # OK Update: @derkveenhof found a simpler workaround: cache the process handle: $p = Start-Process -NoNewWindow -PassThru pwsh '-c whoami'
$dummy = $p.Handle # Cache the handle
$p.WaitForExit()
"Exit code: [$($p.ExitCode)]" # OK However, it should be noted that these workarounds aren't fully equivalent, though it will probably often not make a difference: |
We've just hit this too (as we have PowerShell Core set to auto-update). |
+1 |
2 similar comments
+1 |
+1 |
what the heck, this is pretty bad. How did this slip past testing? I have a number of scripts using '-wait' which are now all bonkers, because the ExitCode and various other properties on System.Diagnostics.Process objects (returned by start-process) are now missing since 7.4 PowerShell update. Can this please be fixed this ASAP |
Weird, is this as known issue too? In one of my scripts I tried swapping from in Start-Process
to a simple call operator ('&'), thinking this would do the trick
And while it works in a Terminal prompt, where the EXE sends prompt output to the terminal and then waits for input (as below)
If I run that same '&' call embedded in a .ps1 script. It seems to have the Input/Output redirection issues, as the same EXE in the same terminal doesn't output the prompt text. Instead it just hangs waiting for input. It still works, but it's confusing as to why the prompt goes missing in that context? |
Ok, got it working similar to what @mklement0 posted in #20400 (comment) The one difference is that to get the same behaviour in terms of screen output when running manually in a terminal AND when running it as part of a larger .ps1, I had to merge the WaitForExit() line (with a semicolon ';' on a single line). If I didn't do this, then the Output (input prompt) from the EXE was showing up on a PS prompt line (with the current directory prefix) when it was run manually in a terminal (but not when run in a large .ps1). I have no idea why that would be the case. But by merging these onto a single line for some reason the EXE prompt goes to a new line (consistent with running the same command in a larger .ps1) So in the end I went from
to (leaving extra lines, as the initoptions as an array might help others)
|
Just to add ... this doesn't happen on MacOS ...
|
Good to know, @softwarebear. |
@jlpetz, @derkveenhof found a simpler workaround - see my updated comment above. |
Just to be consistent ...
|
Thanks. On Unix-like platforms |
We have the same issue on Windows. |
I'm seeing this across a number of PoSh modules and platforms that have PowerShell runners. |
+1 |
+1 on Windows Server 2022 and Windows 11 |
It looks like this will be fixed in v7.4.1: |
This issue has been marked as fixed and has not had any activity for 1 day. It has been closed for housekeeping purposes. |
📣 Hey @unlikelier, how did we do? We would love to hear your feedback with the link below! 🗣️ 🔗 https://aka.ms/PSRepoFeedback |
Prerequisites
Steps to reproduce
I noticed that the
Start-Process
cmdlet sometimes does not populate theExitCode
property in the output, I believe it should.This happed at least when
-PassThru
,-Wait
, and-NoNewWindow
switches are provided.-NoNewWindow
seems to be the reason, since the exit code is available without it.Steps to reproduce
You can see this behavior with this simple test script
Expected behavior
Actual behavior
Error details
No response
Environment data
Visuals
No response
The text was updated successfully, but these errors were encountered: