File size: 2,696 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Diagnostics;
using System.Management.Automation;
using System.Management.Automation.Internal;
namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// This class implements Get-Uptime.
/// </summary>
[Cmdlet(VerbsCommon.Get, "Uptime", DefaultParameterSetName = TimespanParameterSet, HelpUri = "https://go.microsoft.com/fwlink/?linkid=834862")]
[OutputType(typeof(TimeSpan), ParameterSetName = new string[] { TimespanParameterSet })]
[OutputType(typeof(DateTime), ParameterSetName = new string[] { SinceParameterSet })]
public class GetUptimeCommand : PSCmdlet
{
/// <summary>
/// The system startup time.
/// </summary>
/// <value></value>
[Parameter(ParameterSetName = SinceParameterSet)]
public SwitchParameter Since { get; set; } = new SwitchParameter();
/// <summary>
/// This is the main entry point for the cmdlet.
/// </summary>
protected override void ProcessRecord()
{
// Get-Uptime throw if IsHighResolution = false
// because stopwatch.GetTimestamp() return DateTime.UtcNow.Ticks
// instead of ticks from system startup.
// InternalTestHooks.StopwatchIsNotHighResolution is used as test hook.
if (Stopwatch.IsHighResolution && !InternalTestHooks.StopwatchIsNotHighResolution)
{
TimeSpan uptime = TimeSpan.FromSeconds(Stopwatch.GetTimestamp() / Stopwatch.Frequency);
if (Since)
{
// Output the time of the last system boot.
WriteObject(DateTime.Now.Subtract(uptime));
}
else
{
// Output the time elapsed since the last system boot.
WriteObject(uptime);
}
}
else
{
WriteDebug("System.Diagnostics.Stopwatch.IsHighResolution returns 'False'.");
Exception exc = new NotSupportedException(GetUptimeStrings.GetUptimePlatformIsNotSupported);
ThrowTerminatingError(new ErrorRecord(exc, "GetUptimePlatformIsNotSupported", ErrorCategory.NotImplemented, null));
}
}
/// <summary>
/// Parameter set name for Timespan OutputType.
/// </summary>
private const string TimespanParameterSet = "Timespan";
/// <summary>
/// Parameter set name for DateTime OutputType.
/// </summary>
private const string SinceParameterSet = "Since";
}
}
|