Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Diagnostics;
using System.Management;
using System.Management.Automation;
using System.Management.Automation.Internal;

Expand All @@ -28,6 +29,24 @@ public class GetUptimeCommand : PSCmdlet
/// </summary>
protected override void ProcessRecord()
{
#if !UNIX
// We use WMI since Stopwatch has a significant drift on Windows over time.
ManagementObject wmiObject = new ManagementObject(@"\\.\root\cimv2", "Win32_OperatingSystem=@", options: null);
string lastBootUpTime = wmiObject["LastBootUpTime"].ToString();

DateTime lastBootDateTime = ManagementDateTimeConverter.ToDateTime(lastBootUpTime);
switch (ParameterSetName)
{
case TimespanParameterSet:
// return TimeSpan of time since the system started up
WriteObject(DateTime.Now.Subtract(lastBootDateTime));
break;
case SinceParameterSet:
// return Datetime when the system started up
WriteObject(lastBootDateTime);
break;
}
#else
// Get-Uptime throw if IsHighResolution = false
// because stopwatch.GetTimestamp() return DateTime.UtcNow.Ticks
// instead of ticks from system startup.
Expand All @@ -54,6 +73,7 @@ protected override void ProcessRecord()
Exception exc = new NotSupportedException(GetUptimeStrings.GetUptimePlatformIsNotSupported);
ThrowTerminatingError(new ErrorRecord(exc, "GetUptimePlatformIsNotSupported", ErrorCategory.NotImplemented, null));
}
#endif
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,28 @@
Describe "Get-Uptime" -Tags "CI" {
BeforeAll {
$IsHighResolution = [system.diagnostics.stopwatch]::IsHighResolution
# Skip Get-Uptime test if IsHighResolution = false
# Skip Get-Uptime all tests on Unix if IsHighResolution = false
# because stopwatch.GetTimestamp() return DateTime.UtcNow.Ticks
# instead of ticks from system startup
if ( ! $IsHighResolution )
{
$origDefaults = $PSDefaultParameterValues.Clone()
$PSDefaultParameterValues['it:skip'] = $true
}
}
AfterAll {
if ( ! $IsHighResolution ){
$global:PSDefaultParameterValues = $origDefaults
}
# Skip Get-Uptime 'throw' test on Windows since we use WMI there.
}
It "Get-Uptime return timespan (default -Timespan)" {
It "Get-Uptime return timespan (default -Timespan)" -Skip:(! $IsHighResolution -and ! $IsWindows) {
$upt = Get-Uptime
$upt | Should -BeOfType Timespan
}
It "Get-Uptime -Since return DateTime" {
It "Get-Uptime -Since return DateTime" -Skip:(! $IsHighResolution -and ! $IsWindows) {
$upt = Get-Uptime -Since
$upt | Should -BeOfType DateTime
}
It "Get-Uptime throw if IsHighResolution == false" {
It "Get-Uptime throw if IsHighResolution == false" -Skip:(! $IsHighResolution -or $IsWindows) {
# Enable the test hook
[system.management.automation.internal.internaltesthooks]::SetTestHook('StopwatchIsNotHighResolution', $true)

try {
try {
{ Get-Uptime } | Should -Throw -ErrorId "GetUptimePlatformIsNotSupported,Microsoft.PowerShell.Commands.GetUptimeCommand"
} finally {
} finally {
# Disable the test hook
[system.management.automation.internal.internaltesthooks]::SetTestHook('StopwatchIsHighResolutionIsFalse', $false)
}
}
}
}