
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-11-2024 09:47 AM
I have a PowerShell script that shuts down all PCs within a specified time. The script consists of 2 scenarios;
1. A pop-up message to inform the user that the PC will be shut down in 10 minutes and allow a user to cancel the shutdown task if they click on the Cancel button.
and
2. The PC will auto-shutdown if no one is signed in.
Scenario 1 will work if I select "currently logged on" in the deployment. It doesn't work with other selections like "System account" or "Scanning credentials" as the pop-up did not appear.
Scenario 2 will work if I select "System account" in the deployment.
I can't make the deployment work on both scenarios. Is there other way to achieve it? Thank you
- Labels:
-
General Discussion
-
Question

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-10-2025 02:40 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-07-2025 05:36 PM
Hello @AL13
That's a good question -- I think it will require a little testing as I'm not 100% confident.
If you're scanning credential has rights on that computer, you may want to try using the "Scanning Credential" option to see if that makes it run.
But, if you want to ensure the pop-up appears for the logged in user... you may need to do some PowerShell magic. You may need to use specific credentials inside PowerShell. I just don't like that option much at all because it means your creds are in plain text.
Do some testing and let us know how it goes -- I look forward to hearing of what you discover.
Lansweeper Employee
