AutomoxAutomox

Worklet - Reboot Computer as System account

Tom.Aril
November 5, 2020 at 3:29 pm UTC

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)

1 line
Exit 1

Remediation code:

powershell11 lines
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"

This discussion is archived and closed to replies. Continue it in the Automox Community Slack.

Discuss on Slack

Replies (8)

Mike_McNabbNovember 25, 2020
#1

I’m new to Automox. Why do this instead of:
Restart-Computer
?

Tom.ArilNovember 27, 2020
#2

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.

image001.png

image002.jpg

Rich-AutomoxNovember 27, 2020
#3

Restart-Computer -Force

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

Mike_McNabbNovember 27, 2020
#4

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).

Tom.ArilNovember 28, 2020
#5

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?

Mike_McNabbNovember 28, 2020
#6

Yes it should.

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

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

PUT YOUR CODE HERE

Stop-Transcript

Tom.ArilJanuary 4, 2021
#7

I stand corrected, Restart-Computer -Force works. I don’t know why it did not work before creating this worklet, but it works now.
I’ve testet with logged on user, locked computer and logged off user and the computer restarts every time.

Guess this worklet is unnecessary then.

Kind regards
Tom Aril Virak

richierichesAugust 16, 2022
#8

Hi,

I am very new to Automox. 

Please show me how to setup the worklet to reboot both windows and linux endpoints.

Thanks.