-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetUptime.ps
96 lines (80 loc) · 2.8 KB
/
GetUptime.ps
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# Robs custom Get-Uptime function for Powershell 5.1
#
# PowerShell 7 C# cmdlet source https://github.com/PowerShell/PowerShell/blob/master/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetUptime.cs
#
# Usage examples to run at the end of the code.
#
# $startTime = Get-Uptime -Since
#
# $uptime = Get-Uptime
#
# Check if the uptime days is greater than 10 and if so restart the local computer
# $uptime = Get-Uptime
# if ($uptime.Days -gt 10) {
# # This example requires administrative privileges to run
# # Restart-Computer cmdlet will restart the local computer
# # Restart the computer immediately
# Write-Host "The system has been up for more than 10 days, restarting."
# Restart-Computer
# }
# Check if an Get-Uptime cmdlet is found, if Get-Uptime is not found, lets define the custom function.
try {
# Try to get the built-in Get-Uptime cmdlet
$null = Get-Command Get-Uptime -ErrorAction Stop
}
catch {
# If Get-Uptime is not found, define the custom function
function Get-Uptime {
param (
[Switch]$Since
)
if ([System.Diagnostics.Stopwatch]::IsHighResolution) {
$uptimeTicks = [System.Diagnostics.Stopwatch]::GetTimestamp()
$uptimeFrequency = [System.Diagnostics.Stopwatch]::Frequency
$uptimeSeconds = $uptimeTicks / $uptimeFrequency
write-host $uptimeSeconds
$uptime = [TimeSpan]::FromSeconds($uptimeSeconds)
if ($Since) {
return (Get-Date).AddSeconds(-$uptime.TotalSeconds)
} else {
return $uptime
}
} else {
Write-Error "High-resolution timer not available."
}
<#
.SYNOPSIS
Gets the system uptime.
.DESCRIPTION
The Get-Uptime function calculates the time elapsed since the system was last booted.
.SYNTAX
Get-Uptime [<CommonParameters>]
Get-Uptime [-Since] [<CommonParameters>]
.PARAMETERS
-Since
If used, the function returns the DateTime when the system started.
<CommonParameters>
This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer, PipelineVariable, and OutVariable.
.INPUTS
None
.OUTPUTS
System.TimeSpan
If the -Since parameter is not used, the function returns a TimeSpan object representing the system uptime.
System.DateTime
If the -Since parameter is used, the function returns a DateTime object representing when the system was last started.
.EXAMPLE
PS> Get-Uptime
This command gets the current system uptime.
.EXAMPLE
PS> Get-Uptime -Since
This command gets the DateTime when the system was last started.
.NOTES
This is a custom function similar to the Get-Uptime cmdlet available in PowerShell 7 and later.
.LINK
https://go.microsoft.com/fwlink/?linkid=834862
#>
}
}
# Get-Uptime should now be able to be used.