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) {
|
Function Restart-EndpointOnUptime ([int]$MaxUptimeHours) {
|
||||||
|
|
||||||
# Get the last boot up time as a DateTime
|
# Exit if the specified number of hours is not greater than 0
|
||||||
$LastBootUpTime = [Management.ManagementDateTimeConverter]::ToDateTime($(Get-WmiObject -Class Win32_OperatingSystem -ErrorAction Stop | Select-Object -ExpandProperty LastBootUpTime))
|
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
|
# Get the last boot up time as a DateTime
|
||||||
If ($($(Get-Date) - $LastBootUpTime).TotalHours -gt $MaxUptimeHours) {
|
$LastBootTime = [Management.ManagementDateTimeConverter]::ToDateTime($(Get-WmiObject -Class Win32_OperatingSystem -ErrorAction Stop | Select-Object -ExpandProperty LastBootUpTime))
|
||||||
Write-Output "Restarting computer because it has not been restarted in the last ${MaxUptimeHours} hours."
|
|
||||||
#Restart-Computer -Force
|
# 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