Last active 1701039884

_.md Raw

requires PowerShell 7

The script will create a daily scheduled task at 8am to check the uptime and schedule a reboot.

After this is created, open taskschd.msc, go into the settings for the uptime reboot task, and set it like this:

'Run whether user is logged in or not', 'Do not store password'

uptime-reboot.ps1 Raw
1$uptime = Get-Uptime
2$uptimeDays = $uptime.TotalDays
3
4if ($uptimeDays -gt 7) {
5 $rebootTime = (Get-Date).AddHours(24)
6 shutdown.exe /r /t 86400
7}
8
9$taskName = "uptime reboot"
10
11if (!(Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue)) {
12 $action = New-ScheduledTaskAction -Execute "pwsh.exe" -Argument "-NoLogo -ExecutionPolicy Bypass -NoProfile -NonInteractive -File $(Resolve-Path $myinvocation.InvocationName)"
13 $trigger = New-ScheduledTaskTrigger -Daily -At "8:00 AM"
14 $task = Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $taskName -Description "Check uptime; if >7 days, schedule reboot"
15 if (Get-Command New-BurntToastNotification -ErrorAction SilentlyContinue) {
16 New-BurntToastNotification -Text "uptime reboot task created for 8am daily"
17 }
18}