Worklet - Reboot Computer as System account

This Worklet will reboot the computer using Scheduled Task running as System account.

Evaluation code: (returns exit code of 1 so that the policy will always run the remediation code when scheduled)

Exit 1

Remediation code:

New-Item -Path "c:\ProgramData\Amagent" -Name "Restart_Once.cmd" -ItemType "file"
Add-Content -Path "c:\ProgramData\Amagent\Restart_Once.cmd" -Value "shutdown -r -t 30"

$action = New-ScheduledTaskAction -Execute powershell.exe -Argument "-windowstyle hidden c:\ProgramData\Amagent\Restart_Once.cmd"

$time = (Get-Date).AddSeconds(10)
$triggerAt = New-ScheduledTaskTrigger -At $time -Once
Register-ScheduledTask -TaskName "Restart_Once" -Trigger $triggerAt -Action $action -User "NT Authority\SYSTEM"
Start-Sleep 11
Unregister-ScheduledTask -TaskName "Restart_Once" -Confirm:$false
Remove-Item -Path "c:\ProgramData\Amagent\Restart_Once.cmd"

I’m new to Automox. Why do this instead of:

A simple Restart-Computer does not work, thus I had to create this worklet.

Probably because Automox runs as system account, running a restart using task scheduler as system account seems to work tho.



Restart-Computer -Force

Should suffice since it suppresses the user prompt that is probably getting squashed.

Yep. Verified. I tried just Restart-Computer but it wasn’t working, so I tried it with some Transcripts/logging enabled and it was erroring that it wouldn’t reboot because there were users(s) logged in.

Restart-Computer -Force overrides the logged in user(s).

Hm, ok.

Restart-Computer -force did not work for me before I created this Worklet.
Does it work if a user is locked or No user logged on?

Yes it should.

Do something like this to figure out where you’re running into problems:

Stop-Transcript | out-null
$ErrorActionPreference = “Continue”
Start-Transcript -path C:\Windows\temp\My-Log-File.log -append