Title: Process objects created with Start-Process -NoNewWindow and/or -RedirectStandard* never report their exit code on Windows. · Issue #5421 · PowerShell/PowerShell · GitHub
Open Graph Title: Process objects created with Start-Process -NoNewWindow and/or -RedirectStandard* never report their exit code on Windows. · Issue #5421 · PowerShell/PowerShell
X Title: Process objects created with Start-Process -NoNewWindow and/or -RedirectStandard* never report their exit code on Windows. · Issue #5421 · PowerShell/PowerShell
Description: Note: The problem only surfaces on Windows. The problem does not occur when you use -Wait in addition to -PassThru as opposed to a separate Wait-Process call. Update: Per #20400, the problem now (PowerShell 7.4.0-preview.5) also surfaces...
Open Graph Description: Note: The problem only surfaces on Windows. The problem does not occur when you use -Wait in addition to -PassThru as opposed to a separate Wait-Process call. Update: Per #20400, the problem now (P...
X Description: Note: The problem only surfaces on Windows. The problem does not occur when you use -Wait in addition to -PassThru as opposed to a separate Wait-Process call. Update: Per #20400, the problem now (P...
Opengraph URL: https://github.com/PowerShell/PowerShell/issues/5421
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Process objects created with Start-Process -NoNewWindow and/or -RedirectStandard* never report their exit code on Windows.","articleBody":"Note:\r\n\r\n* The problem only surfaces on Windows.\r\n* The problem does _not_ occur when you use `-Wait` in addition to `-PassThru` as opposed to a separate `Wait-Process` call.\r\n * _Update_: Per #20400, the problem now (PowerShell 7.4.0-preview.5) also surfaces when `-Wait` _is_ used. \r\n\r\n**Workaround**:\r\n\r\nPer https://github.com/PowerShell/PowerShell/issues/20716#issuecomment-1817967688, the workaround is to cache the process handle before calling `Wait-Process` / `.WaitForExit()`:\r\n\r\n```powershell\r\n$p = Start-Process cmd -args '/c', 'ver' -PassThru -NoNewWindow\r\n$dummy = $p.Handle # WORKAROUND: use a dummy variable to cache the handle\r\nWait-Process -id $p.Id\r\n\"Exit code: [$($p.ExitCode)]\" # Now the exit code is reported.\r\n```\r\n\r\nSteps to reproduce\r\n------------------\r\n\r\nOn Windows:\r\n\r\n```powershell\r\n$p = Start-Process cmd -args '/c', 'ver' -PassThru -NoNewWindow\r\nWait-Process -id $p.Id\r\n\"Exit code: [$($p.ExitCode)]\"\r\n```\r\n\r\nNote that the problem only occurs when `-NoNewWindows` and /or any of the `-RedirectStandard*` parameters are present.\r\n\r\nInserting a `$p.WaitForExit()` call after the `Wait-Process` call, as suggested [in the docs](https://msdn.microsoft.com/en-us/library/system.diagnostics.process.exitcode%28v=vs.110%29.aspx?f=255\u0026MSPPError=-2147217396) to ensure that `.ExitCode` has a value (which should be the equivalent of `Wait-Process`), doesn't help.\r\n\r\nExpected behavior\r\n-----------------\r\n\r\n```none\r\n\r\nMicrosoft Windows [Version 10.0.15063]\r\nExit code: [0]\r\n```\r\n\r\n`$ps.ExitCode` should contain `0`, given that the `cmd` command reports exit code `0`.\r\n\r\nActual behavior\r\n---------------\r\n\r\n```none\r\n\r\nMicrosoft Windows [Version 10.0.15063]\r\nExit code: []\r\n```\r\n\r\nNote how `$ps.ExitCode` is unexpectedly stringified to the empty string.\r\nAlso, even though `Get-Member` indicates that the property's date type is `[int]`, `$null -eq $ps.ExitCode` is `$true`.\r\n\r\nThis suggests that an exception is occurring behind the scenes, which PowerShell quietly ignores.\r\n\r\nAn exception when accessing `.ExitCode` should only occur if the process hasn't exited yet, however, which is at odds with having used `Wait-Process` and `$p.HasExited` indicating `$true`.\r\n\r\n\r\n\r\n\r\nEnvironment data\r\n----------------\r\n\r\n\u003c!-- provide the output of $PSVersionTable --\u003e\r\n\r\n```powershell\r\nPowerShell Core v6.0.0-beta.9 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)\r\nWindows PowerShell v5.1.15063.674 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)\r\n```\r\n","author":{"url":"https://github.com/mklement0","@type":"Person","name":"mklement0"},"datePublished":"2017-11-11T18:33:36.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":9},"url":"https://github.com/5421/PowerShell/issues/5421"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:15c6b771-6d10-bc97-b72c-8fbcbf1ade26 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D666:339F37:2F6AF47:4433B2D:6A55287B |
| html-safe-nonce | 7662495543ef47cb2c4393e177c3472c5fa1e31a1bd774d1e1e820848f20083f |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENjY2OjMzOUYzNzoyRjZBRjQ3OjQ0MzNCMkQ6NkE1NTI4N0IiLCJ2aXNpdG9yX2lkIjoiMTIxMjkzMjQwMDA2MzI2MDc5NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | d1267214c75812bccfa7a54d4af090b12b144919756da4920abc8c89abbbada0 |
| hovercard-subject-tag | issue:273166144 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/PowerShell/PowerShell/5421/issue_layout |
| twitter:image | https://opengraph.githubassets.com/b9f3c4a9141ba2a428f8287fc3a38c893aa08e8b8af7cd0f14fa6ec861446eab/PowerShell/PowerShell/issues/5421 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/b9f3c4a9141ba2a428f8287fc3a38c893aa08e8b8af7cd0f14fa6ec861446eab/PowerShell/PowerShell/issues/5421 |
| og:image:alt | Note: The problem only surfaces on Windows. The problem does not occur when you use -Wait in addition to -PassThru as opposed to a separate Wait-Process call. Update: Per #20400, the problem now (P... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | mklement0 |
| hostname | github.com |
| expected-hostname | github.com |
| None | 682d273eacb2ac51680c6eb9c0b270f029f7ce74c32090f319083c34497e28a5 |
| turbo-cache-control | no-preview |
| go-import | github.com/PowerShell/PowerShell git https://github.com/PowerShell/PowerShell.git |
| octolytics-dimension-user_id | 11524380 |
| octolytics-dimension-user_login | PowerShell |
| octolytics-dimension-repository_id | 49609581 |
| octolytics-dimension-repository_nwo | PowerShell/PowerShell |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 49609581 |
| octolytics-dimension-repository_network_root_nwo | PowerShell/PowerShell |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 358dd24ad7465d20e3fae729b5b169d35b0dd7f8 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width