Enforce Reboot Policy

  • 27 October 2021
  • 0 replies
  • 5 views

I have been testing the Enforce REboot Policy and it works as designed, however the message either is flashing to fast for me to see or it’s now showing up at all. Below is the code I’m using. Is there anything else I need to do to get the message to show.


$execute = “C:\Windows\System32\msg.exe”

$message = “Machine will reboot in 30 second Please save all work”


$time = (Get-Date).AddSeconds(30)

$triggerAt = New-ScheduledTaskTrigger -At $time -Once

$currentusr = (Get-WmiObject -class win32_process -ComputerName ‘localhost’ | Where-Object name -Match explorer).getowner().user

$argument = $currentusr + " " + “/W” + " " + $message

$command = $execute + " " + $argument + " " + “| Out-Null `n”


New-Item -Path “c:\ProgramData\Amagent” -Name “message.ps1” -ItemType “file” -Value $command

Add-Content -Path “c:\ProgramData\Amagent\message.ps1” -Value “Restart-Computer”


$action = New-ScheduledTaskAction -Execute Powershell.exe -Argument “-windowstyle hidden c:\ProgramData\Amagent\message.ps1”


Register-ScheduledTask -TaskName “StartMsg” -Trigger $triggerAt -Action $action -User $currentusr

Start-Sleep 31

Unregister-ScheduledTask -TaskName “StartMsg” -Confirm:$false

Remove-Item -Path “c:\ProgramData\Amagent\message.ps1”


0 replies

Be the first to reply!

Reply