Hi,
here's a script that you can deploy as System. It will reboot instantly if noone is logged on, otherwise it will display a dialogue that will schedule the reboot for now + 10 minutes if the user clicks ok, if they click cancel it will not reboot.
The script can run as System and will display the prompt for the currently logged in user.
# Get the currently logged on user
$currentUser = (Get-CimInstance -Class Win32_ComputerSystem).UserName
if (-not $currentUser) {
# No user logged on, so immediately reboot
Restart-Computer -Force
}
$taskname = "RebootPrompt"
$scriptblock = {
Add-Type -AssemblyName System.Windows.Forms
# Create a dummy form to act as the owner. Needed to have the message box on top of all windows
$ownerForm = New-Object System.Windows.Forms.Form
$ownerForm.TopMost = $true
$result = [System.Windows.Forms.MessageBox]::Show($ownerForm,
'This computer will restart in 10 minutes. Click "Cancel" to prevent the restart.',
'Restart Warning',
[System.Windows.Forms.MessageBoxButtons]::OKCancel,
[System.Windows.Forms.MessageBoxIcon]::Warning
)
if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
shutdown /r /t 600
}
}
$encodedCommand = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($scriptblock))
$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-WindowStyle Hidden -ExecutionPolicy Bypass -NoProfile -EncodedCommand $encodedCommand"
$principal = New-ScheduledTaskPrincipal -UserId $currentUser -LogonType Interactive
if (Get-ScheduledTask -TaskName $taskname -ErrorAction SilentlyContinue) {
# Task already exists, remove it
Unregister-ScheduledTask -TaskName $taskname -Confirm:$false
}
Register-ScheduledTask -TaskName $taskname -Action $action -Principal $principal
Start-ScheduledTask -TaskName $taskname