# Function to restart an endpoint that has not been restarted for the specified number of hours Function Restart-EndpointOnUptime ([int]$MaxUptimeHours) { # 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 } # 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" } }