_.md
· 341 B · Markdown
Raw
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:

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:

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