Files
management-scripts/Restart-EndpointOnUptime.ps1
T
2024-02-28 14:14:23 -05:00

21 lines
848 B
PowerShell

Function Restart-EndpointOnUptime ([int]$MaxUptimeHours) {
# Check the last boot up time
#$LastBootUpTime =
$LastBootUpTime = [Management.ManagementDateTimeConverter]::ToDateTime($(Get-WmiObject -Class Win32_OperatingSystem -ErrorAction Stop | Select-Object -ExpandProperty LastBootUpTime))
# Calculate the difference in hours
#$CurrentTime =
#$TimeDifference =
#$DifferenceInHours =
# Check if the difference is greater than $MaxUptimeHours
If ($($(Get-Date) - $LastBootUpTime).TotalHours -gt $MaxUptimeHours) {
# Restart the computer
Write-Output "Restarting computer because it has not been restarted in the last ${MaxUptimeHours} hours."
#Restart-Computer
} Else {
Write-Output "No need to restart. The last restart was within ${MaxUptimeHours} hours."
}
}