0

I want to run a shutdown command on a remote computer, but I want the user to be prompted to accept or decline the command. This is what I have. It prompts the local user for the $computername, I need to run an input prompt on the remote computer user, then execute the shutdown script.

$server=read-host 'Enter Name'
$choice = ""
while ($choice -notmatch "[y|n]"){
$choice = read-host "Do you want to continue? (Y/N)"
}
if($choice -eq "y"){
shutdown /m \\$server /r /t 00
}
else {write-host "Please contact your systems administrator."}
3
  • 1
    this clearly isn't the way to go, not sure how you would achieve that, tbh, probably invoke-command and some winforms magic? I don't know Commented Jan 25, 2017 at 19:19
  • The biggest problem you are going to run into is running this interactively in the user's session. Making a scheduled task is most common method. Commented Jan 25, 2017 at 19:56
  • Yes, the interactive login makes the shutdown command fail unless I -force. Commented Jan 25, 2017 at 20:05

1 Answer 1

1

The biggest problem is displaying the message to the user because it has to run interactively in that user session.

One method, if you just want to display a message that the computer is about to shutdown, and you don't want them to be able to cancel, would be to leverage the remote shutdown command:

shutdown -m //computername -r -f -c "MESSAGE" -t 120

If you need more comprehensive shutdown, where the user can always cancel (and believe me, they will always choose to cancel) the shutdown, then you need to use something like PsExec where you run a script in the interactive session:

psexec.exe \\computername -I message.vbs

Where message.vbs could be whatever more complex script (or PowerShell script) that you need to run.

Sign up to request clarification or add additional context in comments.

2 Comments

In the end I decided to use the shutdown command and prompt the user that they have 5 minutes to save their work before they are forced to shutdown. There is no way to invoke. Maybe pssexec, but i didn't want to deal with sysinternals.
$server=read-host 'Enter Hostname' while ($choice -notmatch "[y|n]"){ $choice = read-host "If you want to immediately shutdown the remote computer select yes or if you want to give the user 5 minutes to prepare for shutdown select no. (Y/N)"} if($choice -eq "y"){ shutdown /s /m \\$server /t 00 } if($choice -eq "n"){ shutdown /s /m \\$server /f /c "Your session needs close immediately for network security reasons. Please close this message and save your work. The system will shutdown in 5 minutes." /t 300 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.