Last active 1701039884

aly's Avatar aly revised this gist 1701039884. Go to revision

1 file changed, 2 insertions

_.md

@@ -1,3 +1,5 @@
1 + requires [PowerShell 7](https://learn.microsoft.com/en-us/powershell/scripting/install/installing-powershell-on-windows?view=powershell-7.4)
2 +
1 3 The script will create a daily scheduled task at 8am to check the uptime and schedule a reboot.
2 4
3 5 After this is created, open `taskschd.msc`, go into the settings for the `uptime reboot` task, and set it like this:

aly's Avatar aly revised this gist 1701039802. Go to revision

1 file changed, 3 insertions, 1 deletion

uptime-reboot.ps1

@@ -12,5 +12,7 @@ if (!(Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue)) {
12 12 $action = New-ScheduledTaskAction -Execute "pwsh.exe" -Argument "-NoLogo -ExecutionPolicy Bypass -NoProfile -NonInteractive -File $(Resolve-Path $myinvocation.InvocationName)"
13 13 $trigger = New-ScheduledTaskTrigger -Daily -At "8:00 AM"
14 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"
15 + if (Get-Command New-BurntToastNotification -ErrorAction SilentlyContinue) {
16 + New-BurntToastNotification -Text "uptime reboot task created for 8am daily"
17 + }
16 18 }

aly's Avatar aly revised this gist 1701039736. Go to revision

1 file changed, 10 insertions, 8 deletions

uptime-reboot.ps1

@@ -1,14 +1,16 @@
1 - # Check system uptime
2 1 $uptime = Get-Uptime
3 2 $uptimeDays = $uptime.TotalDays
4 3
5 - # Check if uptime is greater than 7 days
6 4 if ($uptimeDays -gt 7) {
7 - Write-Host "System has been up for $($uptimeDays) days. Scheduling a reboot in 24 hours."
8 -
9 - # Schedule reboot in 24 hours
10 5 $rebootTime = (Get-Date).AddHours(24)
11 - shutdown.exe /r /t 86400 # 86400 seconds = 24 hours
12 - } else {
13 - Write-Host "System uptime is $($uptimeDays) days. No reboot scheduled."
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"
14 16 }

aly's Avatar aly revised this gist 1701039658. Go to revision

1 file changed, 1 insertion, 1 deletion

_.md

@@ -2,4 +2,4 @@ The script will create a daily scheduled task at 8am to check the uptime and sch
2 2
3 3 After this is created, open `taskschd.msc`, go into the settings for the `uptime reboot` task, and set it like this:
4 4
5 - !["Run whether user is logged in or not", "Do not store password"](https://s3.aly.pet/ss/Screenshot%202023-11-27%20085124.png)
5 + !['Run whether user is logged in or not', 'Do not store password'](https://s3.aly.pet/ss/Screenshot%202023-11-27%20085124.png)

aly's Avatar aly revised this gist 1701039632. Go to revision

2 files changed, 19 insertions

_.md(file created)

@@ -0,0 +1,5 @@
1 + The script will create a daily scheduled task at 8am to check the uptime and schedule a reboot.
2 +
3 + After this is created, open `taskschd.msc`, go into the settings for the `uptime reboot` task, and set it like this:
4 +
5 + !["Run whether user is logged in or not", "Do not store password"](https://s3.aly.pet/ss/Screenshot%202023-11-27%20085124.png)

uptime-reboot.ps1(file created)

@@ -0,0 +1,14 @@
1 + # Check system uptime
2 + $uptime = Get-Uptime
3 + $uptimeDays = $uptime.TotalDays
4 +
5 + # Check if uptime is greater than 7 days
6 + if ($uptimeDays -gt 7) {
7 + Write-Host "System has been up for $($uptimeDays) days. Scheduling a reboot in 24 hours."
8 +
9 + # Schedule reboot in 24 hours
10 + $rebootTime = (Get-Date).AddHours(24)
11 + shutdown.exe /r /t 86400 # 86400 seconds = 24 hours
12 + } else {
13 + Write-Host "System uptime is $($uptimeDays) days. No reboot scheduled."
14 + }
Newer Older