Skip to content

Get-Uptime -Since ignores an explicit $false value #25015

@surfingoldelephant

Description

@surfingoldelephant

Prerequisites

Steps to reproduce

(Get-Uptime).GetType().Name               # TimeSpan
(Get-Uptime -Since).GetType().Name        # DateTime
(Get-Uptime -Since:$false).GetType().Name # DateTime, should be TimeSpan

Expected behavior

# TimeSpan
# DateTime
# TimeSpan

Actual behavior

# TimeSpan
# DateTime
# DateTime

Context

  • Get-Uptime emits a [timespan] object by default or [datetime] with -Since.
  • If -Since is explicitly provided a $false value (-Since:$false/hash table splatting), the type is unexpectedly [datetime].
    • -Since/-Since:$true and -Since:$false result in identical behavior.
  • Get-Uptime uses the selected parameter set to determine output type, which is tied to the presence of the -Since switch, not its value.

switch (ParameterSetName)
{
case TimespanParameterSet:
// return TimeSpan of time since the system started up
WriteObject(uptime);
break;
case SinceParameterSet:
// return Datetime when the system started up
WriteObject(DateTime.Now.Subtract(uptime));
break;
}

Despite it being uncommon to specify an explicit $false switch (and arguably against its intention), I believe the current Get-Uptime behavior still amounts to a bug.

  • Specifying $false is permitted by the language and the Get-Uptime result subverts reasonable user expectation.
  • There are valid use cases for this (e.g., dynamically retrieving the boolean from another source).
  • Other cmdlets with parameter sets tied to a switch respect an explicit $false value (e.g., 'foo' | Select-String f -Raw:$false)
# Simulate dynamic retrieval of the boolean.
$getDateTimeOutput = $false

Get-Uptime -Since:$getDateTimeOutput
# Or...
$params = @{ Since = $getDateTimeOutput }
Get-Uptime @params

# Get-Uptime still unexpectedly emits [datetime] output.

Environment data

Name                           Value
----                           -----
PSVersion                      7.6.0-preview.2
PSEdition                      Core
GitCommitId                    7.6.0-preview.2
OS                             Microsoft Windows 10.0.19045
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0, 5.0, 5.1…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-BugIssue has been identified as a bug in the productUp-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Cmdletsgeneral cmdlet issuesWG-ReviewedA Working Group has reviewed this and made a recommendation

    Type

    No type

    Projects

    Status

    Reviewed

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions