Unregister task scheduled for future time (after execution)

Hey all,

Working on a script to find servers w/ a lastboottime > 30 days. Once its identified those its queries for active users and does 1 of 2 things. 1) If active creates a task for 1am the following day to reboot or 2) If not active creates a task for 1 minute from the current time and reboots right away. What im struggling with is how to not leave a left over task on the server. For the task that reboots right away it seems like just using start-sleep gets the job done just fine BUT when I need to schedule it for 1am the following day I cant let it sleep for hours on end while it waits for that time to be reached. Is there any other way to unregister a task only after its been executed without relying on time? Heres my setup so far

$LastReboot = (Get-CimInstance win32_operatingsystem).LastBootUpTime
If($LastReboot -le (get-date).AddDays(-30)) {
$ActiveUser = Quser }

If user found schedule reboot task for following day 1AM (Server time)

If($ActiveUser) {

[Int]$WaitSeconds = (([datetime]‘1AM’).AddHours(24) - (get-date)).totalseconds
$Action = New-ScheduledTaskAction -Execute ‘C:\Windows\System32\shutdown.exe’ -Argument ‘/r /t 300 /c /f’
$Trigger = New-ScheduledTaskTrigger -Once -At (Get-Date).AddSeconds($WaitSeconds)
$Principal = New-ScheduledTaskPrincipal -UserID “NT AUTHORITY\SYSTEM” -LogonType ServiceAccount -RunLevel Highest
$Settings = New-ScheduledTaskSettingsSet -MultipleInstances Parallel
Register-ScheduledTask -TaskName AutoReboot -Action $Action -Trigger $Trigger -Principal $Principal -Settings $Settings

}}

If user NOT found schedule reboot task for 1 minute from current time

Else {

$Action = New-ScheduledTaskAction -Execute ‘C:\Windows\System32\shutdown.exe’ -Argument ‘/r /t 300 /c /f’
$Trigger = New-ScheduledTaskTrigger -Once -At (Get-Date).AddMinutes(1)
$Principal = New-ScheduledTaskPrincipal -UserID “NT AUTHORITY\SYSTEM” -LogonType ServiceAccount -RunLevel Highest
$Settings = New-ScheduledTaskSettingsSet -MultipleInstances Parallel
Register-ScheduledTask -TaskName AutoReboot -Action $Action -Trigger $Trigger -Principal $Principal -Settings $Settings
Start-Sleep -Seconds 75
Unregister-ScheduledTask -TaskName AutoReboot -Confirm:$False
}

For no User, if you dont want to leave a task, why create one? Can you simply use shutdown.exe? I would also propose the same for the user found as well, just adjust /t based on some time math ?? I am guessing maybe shutdown.exe may not survive the end of the script? I have not tried/tested.

[quote quote=209547]For no User, if you dont want to leave a task, why create one? Can you simply use shutdown.exe? I would also propose the same for the user found as well, just adjust /t based on some time math ?? I am guessing maybe shutdown.exe may not survive the end of the script? I have not tried/tested.

[/quote]

I wish. No ability to reboot a server directly because my permissions are restrictive af. This was the workaround I came up with. Ive tested it for no active user and even after unregistering the task it still executes the shutdown because of that argument /t 300 so at least I know that is working.

Just thinking out loud here, but what about something like this straight from PS:

-Execute ‘%ComSpec% /c C:\Windows\System32\shutdown.exe /r /t 300 /c /f && c:\windows\system32\schtasks.exe /delete /TN AutoReboot’

And leave off the -Arguments

&& executes the second item if the first succeeds. You might also simply try & which wont wait.

I have not tested this, it is 100% theory/guessing. You may need to mess with quotes here and there.

Let me know the results, I am curious if you can nuke the task you are running from. My guess is not, but maybe worth a try? I guess if you have permissions to create the task, you can also delete?

??

So basically launching cmd to execute the shutdown and once that completes && cleans it up. CMD for the win thank you for the tip! Ive tested and this worked great but had to tweak a few things. Final -execute looks like

-Execute ‘%ComSpec% -Argument /c C:\Windows\System32\shutdown.exe /r /t 300 /c /f && c:\windows\system32\schtasks.exe /delete /TN AutoReboot /f’

 

Good to hear, and thanks for the updated solution.

[quote quote=209676]Good to hear, and thanks for the updated solution.

[/quote]

Actually I was half way right.

This:

/c C:\Windows\System32\shutdown.exe /r /t 300 /c /f && c:\windows\system32\schtasks.exe /delete /TN AutoReboot /f’

needed to be added as an argument for it to run properly BUT the -argument parameter isnt passing it to the argument field in tasksched. When it worked earlier I had configured by hand (pic below). So it works but CMD is including the argument in the program/script field which throws errors about invalid parameters. Just need to figure out how to pass those right and it should be good.

https://imgur.com/a/JSRDbbk

This did it - $Action = New-ScheduledTaskAction -Execute ‘%ComSpec%’ -Argument '/c C:\Windows\System32\Shutdown.exe /r /t 300 /c /f && C:\Windows\System32\Schtasks.exe /delete /TN AutoReboot /f’