added function Restart-EndpointOnUptime

This commit is contained in:
2023-09-07 10:59:55 -04:00
parent 6a6c3fc9cf
commit 0360329312
+18
View File
@@ -873,5 +873,23 @@ Function Uninstall-MSI ([Parameter(ValueFromPipeline=$true)][string[]]$APP_NAMES
}
}
# Reboot an endpoint based on the amount of time it has been running without a reboot
Function Restart-EndpointOnUptime ([timespan]$MaxUptime) {
# Get the length of time the endpoint has been running without restart
Try { $LastBootTime = (Get-CimInstance -ClassName Win32_OperatingSystem -ErrorAction Stop).LastBootUpTime }
Catch { LogErr $_.Exception.Message ; Exit }
# Get the difference between the endpoint uptime and the current time
$Delta = New-TimeSpan -Start $LastBootTime -End $(Get-Date)
# Stop the function if the endpoint has not exceeded $MaxUptime
If ( $Delta -lt $MaxUptime ) { Exit }
# Reboot the endpoint
Try { Restart-Computer -Force -ErrorAction Stop }
Catch { LogErr $_.Exception.Message }
}
Setup-MSPDirectories
Setup-LogFile