_.md
· 483 B · Markdown
Raw
requires [PowerShell 7](https://learn.microsoft.com/en-us/powershell/scripting/install/installing-powershell-on-windows?view=powershell-7.4)
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:

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:

uptime-reboot.ps1
· 818 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"
if (Get-Command New-BurntToastNotification -ErrorAction SilentlyContinue) {
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 | if (Get-Command New-BurntToastNotification -ErrorAction SilentlyContinue) { |
| 16 | New-BurntToastNotification -Text "uptime reboot task created for 8am daily" |
| 17 | } |
| 18 | } |