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
Prerequisites
Steps to reproduce
Expected behavior
Actual behavior
Context
Get-Uptimeemits a[timespan]object by default or[datetime]with-Since.-Sinceis explicitly provided a$falsevalue (-Since:$false/hash table splatting), the type is unexpectedly[datetime].-Since/-Since:$trueand-Since:$falseresult in identical behavior.Get-Uptimeuses the selected parameter set to determine output type, which is tied to the presence of the-Sinceswitch, not its value.PowerShell/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetUptime.cs
Lines 39 to 49 in df248bd
Despite it being uncommon to specify an explicit
$falseswitch (and arguably against its intention), I believe the currentGet-Uptimebehavior still amounts to a bug.$falseis permitted by the language and theGet-Uptimeresult subverts reasonable user expectation.$falsevalue (e.g.,'foo' | Select-String f -Raw:$false)Environment data