Files
management-scripts/Restart-EndpointOnUptime.ps1
T

11 lines
629 B
PowerShell

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))
# 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
}
}