From 452e78ad0f6e811d356f01ead1338a96788996dc Mon Sep 17 00:00:00 2001 From: Jonathan Gilbert Date: Thu, 2 Oct 2025 22:59:15 -0500 Subject: [PATCH 1/4] Added test of Get-Uptime -Since:$false to Get-Uptime.Tests.ps1, reproducing the bug described in issue #25015. Updated the implementation in GetUptime.cs to correct the handling of explicit $false values passed to -Since, making the test pass. --- .../commands/utility/GetUptime.cs | 17 ++++++++--------- .../Get-Uptime.Tests.ps1 | 4 ++++ 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetUptime.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetUptime.cs index e8dcfbe254a..a7903393082 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetUptime.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetUptime.cs @@ -36,16 +36,15 @@ protected override void ProcessRecord() { TimeSpan uptime = TimeSpan.FromSeconds(Stopwatch.GetTimestamp() / Stopwatch.Frequency); - switch (ParameterSetName) + if (Since.ToBool()) { - 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; + // return Datetime when the system started up + WriteObject(DateTime.Now.Subtract(uptime)); + } + else + { + // return TimeSpan of time since the system started up + WriteObject(uptime); } } else diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Uptime.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Uptime.Tests.ps1 index aff5e4d50af..b3275a61f2a 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Uptime.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Uptime.Tests.ps1 @@ -25,6 +25,10 @@ Describe "Get-Uptime" -Tags "CI" { $upt = Get-Uptime -Since $upt | Should -BeOfType DateTime } + It "Get-Uptime -Since:`$false return TimeSpan" { + $upt = Get-Uptime -Since:$false + $upt | Should -BeOfType TimeSpan + } It "Get-Uptime throw if IsHighResolution == false" { # Enable the test hook [system.management.automation.internal.internaltesthooks]::SetTestHook('StopwatchIsNotHighResolution', $true) From 9c91de9cd8e36a42bce1e91d495fd9cac4738aae Mon Sep 17 00:00:00 2001 From: Jonathan Gilbert Date: Fri, 3 Oct 2025 14:33:51 -0500 Subject: [PATCH 2/4] Changed comments in GetUptime.cs from documenting what it's doing to documenting why it's doing it. --- .../commands/utility/GetUptime.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetUptime.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetUptime.cs index a7903393082..878373dd3d6 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetUptime.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetUptime.cs @@ -38,12 +38,12 @@ protected override void ProcessRecord() if (Since.ToBool()) { - // return Datetime when the system started up + // Caller requests absolute point in time of last boot WriteObject(DateTime.Now.Subtract(uptime)); } else { - // return TimeSpan of time since the system started up + // Caller requests span of time since last boot WriteObject(uptime); } } From 1d92799a1e42151226b6cebb666ddd8ac50c856b Mon Sep 17 00:00:00 2001 From: Jonathan Gilbert Date: Sun, 5 Oct 2025 14:52:58 -0500 Subject: [PATCH 3/4] Use implicit conversion of SwitchParameter instead of ToBool Co-authored-by: xtqqczze <45661989+xtqqczze@users.noreply.github.com> --- .../commands/utility/GetUptime.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetUptime.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetUptime.cs index 878373dd3d6..48df31a6b4f 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetUptime.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetUptime.cs @@ -36,7 +36,7 @@ protected override void ProcessRecord() { TimeSpan uptime = TimeSpan.FromSeconds(Stopwatch.GetTimestamp() / Stopwatch.Frequency); - if (Since.ToBool()) + if (Since) { // Caller requests absolute point in time of last boot WriteObject(DateTime.Now.Subtract(uptime)); From 6f051750c1710f7afc48e6951daf44bfa93db9ed Mon Sep 17 00:00:00 2001 From: Jonathan Gilbert Date: Sun, 5 Oct 2025 16:41:33 -0500 Subject: [PATCH 4/4] Fine tuning of comments per PR feedback Co-authored-by: xtqqczze <45661989+xtqqczze@users.noreply.github.com> --- .../commands/utility/GetUptime.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetUptime.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetUptime.cs index 48df31a6b4f..c21165301e2 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetUptime.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetUptime.cs @@ -38,12 +38,12 @@ protected override void ProcessRecord() if (Since) { - // Caller requests absolute point in time of last boot + // Output the time of the last system boot. WriteObject(DateTime.Now.Subtract(uptime)); } else { - // Caller requests span of time since last boot + // Output the time elapsed since the last system boot. WriteObject(uptime); } }