From 4c5f608457a6f8e5141a317c5ad7c5a52cbfd7d2 Mon Sep 17 00:00:00 2001 From: David Yoder Date: Wed, 28 Feb 2024 13:51:44 -0500 Subject: [PATCH] initial commit --- Restart-EndpointOnUptime.ps1 | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 Restart-EndpointOnUptime.ps1 diff --git a/Restart-EndpointOnUptime.ps1 b/Restart-EndpointOnUptime.ps1 new file mode 100644 index 0000000..3be00bf --- /dev/null +++ b/Restart-EndpointOnUptime.ps1 @@ -0,0 +1,20 @@ +Function Restart-EndpointOnUptime ([timespan]$MaxUptime) { + + # Check the last boot up time + $LastBootUpTime = Get-WmiObject -Class Win32_OperatingSystem | Select-Object -ExpandProperty LastBootUpTime + $LastBootUpTime = [Management.ManagementDateTimeConverter]::ToDateTime($LastBootUpTime) + + # Calculate the difference in hours + $CurrentTime = Get-Date + $TimeDifference = $CurrentTime - $LastBootUpTime + $DifferenceInHours = $TimeDifference.TotalHours + + # Check if the difference is greater than $MaxUptime + If ($DifferenceInHours -gt $MaxUptime) { + # Restart the computer + Write-Output "Restarting computer because it has not been restarted in the last ${MaxUptime} hours." + #Restart-Computer + } Else { + Write-Output "No need to restart. The last restart was within ${MaxUptime} hours." + } +} \ No newline at end of file