Skip to main content

Enforce Reboot Policy

  • October 27, 2021
  • 0 replies
  • 122 views

Forum|alt.badge.img

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


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings