reduce some variables

This commit is contained in:
2024-02-28 14:14:23 -05:00
parent 80877ded48
commit 5167cdcd76
+7 -7
View File
@@ -1,20 +1,20 @@
Function Restart-EndpointOnUptime ([int]$MaxUptimeHours) { Function Restart-EndpointOnUptime ([int]$MaxUptimeHours) {
# Check the last boot up time # Check the last boot up time
$LastBootUpTime = Get-WmiObject -Class Win32_OperatingSystem1 -ErrorAction Stop | Select-Object -ExpandProperty LastBootUpTime #$LastBootUpTime =
$LastBootUpTime = [Management.ManagementDateTimeConverter]::ToDateTime($LastBootUpTime) $LastBootUpTime = [Management.ManagementDateTimeConverter]::ToDateTime($(Get-WmiObject -Class Win32_OperatingSystem -ErrorAction Stop | Select-Object -ExpandProperty LastBootUpTime))
# Calculate the difference in hours # Calculate the difference in hours
$CurrentTime = Get-Date #$CurrentTime =
$TimeDifference = $CurrentTime - $LastBootUpTime #$TimeDifference =
$DifferenceInHours = $TimeDifference.TotalHours #$DifferenceInHours =
# Check if the difference is greater than $MaxUptimeHours # Check if the difference is greater than $MaxUptimeHours
If ($DifferenceInHours -gt $MaxUptimeHours) { If ($($(Get-Date) - $LastBootUpTime).TotalHours -gt $MaxUptimeHours) {
# Restart the computer # Restart the computer
Write-Output "Restarting computer because it has not been restarted in the last ${MaxUptimeHours} hours." Write-Output "Restarting computer because it has not been restarted in the last ${MaxUptimeHours} hours."
Restart-Computer #Restart-Computer
} Else { } Else {
Write-Output "No need to restart. The last restart was within ${MaxUptimeHours} hours." Write-Output "No need to restart. The last restart was within ${MaxUptimeHours} hours."
} }