Skip to content

Stop-Computer, Restart-Computer on Unix: inability to detect failure. #19802

@mklement0

Description

@mklement0

Prerequisites

Steps to reproduce

On Unix, these commands defer to the native /sbin/shutdown utility.

However, the way in which this is currently implemented makes it impossible to detect failure programmatically:

E.g., on macOS, without using sudo, run the following:

# Run *without* sudo, so as to provoke failure of /sbin/shutdown
Stop-Computer -ErrorVariable e 2>$null || Write-Error "Shutdown initiation failed."

Note that running /sbin/shutdown directly in the scenario above:

  • prints shutdown: NOT super-user to stderr
  • reports 1 as its exit code

Expected behavior

No visible output (due to 2>$null), the error message stored in $e, and $? indicating $false so that the || clause is triggered.

Actual behavior

shutdown's stderr prints directly to the terminal, bypassing all PowerShell streams.
$? reflects $true afterwards.

In other words: there is no way to detect failure.

Error details

No response

Environment data

PowerShell 7.4.0-preview.3 on macOS and Linux

Visuals

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    In-PRIndicates that a PR is out for the issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions