Only the changes i made for this is the “message to the sender”…
$execute = “C:\Windows\System32\msg.exe”
$message = “We have patched a security updates on your devices. Please reboot your workstation to take effect.”
$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”