I think this is it
This commit is contained in:
@@ -1,11 +1,21 @@
|
||||
# Function to restart an endpoint that has not been restarted for the specified number of hours
|
||||
Function Restart-EndpointOnUptime ([int]$MaxUptimeHours) {
|
||||
|
||||
# Get the last boot up time as a DateTime
|
||||
$LastBootUpTime = [Management.ManagementDateTimeConverter]::ToDateTime($(Get-WmiObject -Class Win32_OperatingSystem -ErrorAction Stop | Select-Object -ExpandProperty LastBootUpTime))
|
||||
# Exit if the specified number of hours is not greater than 0
|
||||
If ( !($MaxUptimeHours -gt 0) ) { Write-Output "Must specify 1 or more hours" ; Exit }
|
||||
|
||||
# If the number of hours the system has been up is greater than $MaxUptimeHours, restart the endpoint
|
||||
If ($($(Get-Date) - $LastBootUpTime).TotalHours -gt $MaxUptimeHours) {
|
||||
Write-Output "Restarting computer because it has not been restarted in the last ${MaxUptimeHours} hours."
|
||||
#Restart-Computer -Force
|
||||
# Get the last boot up time as a DateTime
|
||||
$LastBootTime = [Management.ManagementDateTimeConverter]::ToDateTime($(Get-WmiObject -Class Win32_OperatingSystem -ErrorAction Stop | Select-Object -ExpandProperty LastBootUpTime))
|
||||
|
||||
# Get the total number of hours the endpoint has been up
|
||||
$UptimeHours = $($(Get-Date) - $LastBootTime).TotalHours
|
||||
|
||||
# If $Uptime is greater than $MaxUptimeHours, restart the endpoint
|
||||
If ( $UptimeHours -gt $MaxUptimeHours) {
|
||||
Write-Output "This endpoint has been up for at least ${UptimeHours} hours and will be restarted now"
|
||||
Start-Sleep -Seconds 5
|
||||
Restart-Computer -Force
|
||||
} Else {
|
||||
Write-Output "This endpoint has already been restarted within the last ${MaxUptimeHours} hours"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user