Community FAQ
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
AL13
Engaged Sweeper

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

 

2 REPLIES 2
ThoKl
Engaged Sweeper II

 

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
Tim_N
Lansweeper Employee
Lansweeper Employee

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. 

 

Tim N.
Lansweeper Employee

General Discussions

Find answers to technical questions about Lansweeper.

New to Lansweeper?

Try Lansweeper For Free

Experience Lansweeper with your own data.
Sign up now for a 14-day free trial.

Try Now