8000 Process objects created with Start-Process -NoNewWindow and/or -RedirectStandard* never report their exit code on Windows. · Issue #5421 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content
Process objects created with Start-Process -NoNewWindow and/or -RedirectStandard* never report their exit code on Windows. #5421
@mklement0

Description

@mklement0

Note:

Workaround:

Per #20716 (comment), the workaround is to cache the process handle before calling Wait-Process / .WaitForExit():

$p = Start-Process cmd -args '/c', 'ver' -PassThru -NoNewWindow
$dummy = $p.Handle # WORKAROUND: use a dummy variable to cache the handle
Wait-Process -id $p.Id
"Exit code: [$($p.ExitCode)]"  # Now the exit code is reported.

Steps to reproduce

On Windows:

$p = Start-Process cmd -args '/c', 'ver' -PassThru -NoNewWindow
Wait-Process -id $p.Id
"Exit code: [$($p.ExitCode)]"

Note that the problem only occurs when -NoNewWindows and /or any of the -RedirectStandard* parameters are present.

Inserting a $p.WaitForExit() call after the Wait-Process call, as suggested in the docs to ensure that .ExitCode has a value (which should be the equivalent of Wait-Process), doesn't help.

Expected behavior


Microsoft Windows [Version 10.0.15063]
Exit code: [0]

$ps.ExitCode should contain 0, given that the cmd command reports exit code 0.

Actual behavior


Microsoft Windows [Version 10.0.15063]
Exit code: []

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 used Wait-Process and $p.HasExited indicating $true.

Environment data

PowerShell Core v6.0.0-beta.9 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)
Windows PowerShell v5.1.15063.674 on Microsoft Windows 10 Pro (64-bit; v10.0.15063)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-No ActivityIssue has had no activity for 6 months or moreWG-Cmdlets-Managementcmdlets in the Microsoft.PowerShell.Management module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0