# PRTG-Reboot # [cmdletbinding()] param ( [UInt64]$MinFreeLimit = 50000, # report Machine if FreePhysicalMemory to low [UInt64]$looprotect = 60 # prevent reboot if uptime minutes lower than looprotect ) write-verbose "prtg-reboot: start" [int]$prtgcode=0 # exit code default write-verbose "prtg-reboot: Loading Memory" $os = Get-Ciminstance Win32_OperatingSystem Write-verbose "prtg-reboot: Get SystemUptime" [UInt64]$UptimeMinutes = [UInt64]($OS.LocalDateTime - $OS.LastBootUpTime).totalminutes [UInt64]$Uptimehours = [UInt64]($OS.LocalDateTime - $OS.LastBootUpTime).totalhours write-verbose "prtg-reboot: Calculating Values" [int]$percentFree = [int][math]::Round(($os.FreePhysicalMemory/$os.TotalVisibleMemorySize)*100,0) [UInt64]$freemen=$os.FreePhysicalMemory write-verbose "prtg-reboot: Checking Memory Status $($percentFree)" [string]$Status="unknown" if ($percentFree -ge 25) { $Status = "MemOK" } elseif ($percentFree -ge 15 ) { $Status = "MemWarn" } else { $Status = "MemCrit" } $checkMemLimit = $freemen - $MinFreeLimit if ($checkMemLimit -lt "0"){ if ($UptimeMinutes -lt $looprotect) { $Status += " LoopProtect" } else { $Status += " Restart" } } $prtgresult = ' Percent Memory Free '+$percentFree+' TotalVisibleMemorySize '+$os.TotalVisibleMemorySize+' FreePhysicalMemory '+$freemen+' MinFreeLimit '+$MinFreeLimit+' Uptime in Stunden '+$Uptimehours+' '+$Status+' ' write-verbose "Send PRTG-Result to STD-Out" $prtgresult if ($checkMemLimit -lt "0"){ if ($UptimeMinutes -lt $looprotect) { write-verbose "prtg-reboot: RESTART delayed due LoopProtect" $Status += " LoopProtect" $prtgcode=1 # Warning } else { $Status += " Restart" write-verbose "prtg-reboot: RESTART Computer in 30 Sec" shutdown /r /f /t 30 /c "Restart scheduled due PRTG-SensorCheck" /d p:4:6 # /r = Rebootlimit # /f = Force # /t 30 Timeout to 30 Sec # /c Message to show # /d Shutdowncode Expected, planned appliation instabil $prtgcode=4 # set sensor to error } } else { write-verbose "prtg-reboot: No Reboot scheduled" } write-verbose "prtg-reboot: End" exit ($prtgcode)