Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?

Turn Off remote computers with PowerShell

Posted on 2017-10-12
6
High Priority
?
38 Views
Last Modified: 2017-10-18
Hi,

I'm working on a script that will shut down domain PCs at a certain time.  I need to prompt users (wscript.shell) that this is happening, and give them a chance to cancel the shutdown.  Because of this I'm using Invoke-Command to run the code on the remote PCs.

The issue is, the remotely logged in users are not receiving the prompts.  How can I direct the wscript.shell object to appear to the remote users?

...
foreach ($ComputerName in $Computers) {
    Invoke-Command -ComputerName $ComputerName.cn  -ScriptBlock {
        function YesNoForm(){
            [CmdletBinding()]
            param()
           
            Begin{
                $a = New-Object -ComObject wscript.shell
            }
           
            Process{
                $intAnswer = $a.Popup("Computer is shutting down, press 'No' to abort",60,"Shutdown",4)
            }
           
            End{
                return $intAnswer
            }
        }
       
        $answer = YesNoForm
        if($answer -ne 7){
            Stop-Computer  -Force
        }
    }
}
0
Comment
Question by:Oscar Powers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 14
ID: 42329316
Consider using the CMD "Shutdown" instead as it works against remote computers and automatically supplies the pop-up about the shut-down with the option to abort it to all users logged into the system.

Example code of this in CMD:

shutdown /s /m \\YourCompuer.Yourdomain.com /t 60 /d p:1:1 /c "Administrative Shutdown of System"

Open in new window


Wrapped in a powershell for you:

foreach ($ComputerName in $Computers) {
     Invoke-Command -ScriptBlock {& shutdown /s /m \\$($ComputerName.cn) /t 60 /d p:1:1 /c "Administrative Shutdown of System" }
}

Open in new window

2
 
LVL 1

Author Comment

by:Oscar Powers
ID: 42329330
Thanks Ben:

I try it but I can not get a prompt with the option to cancel.  Also i need to shutdown like a 1000 PCs
0
 
LVL 80

Expert Comment

by:arnold
ID: 42329345
shutdown does not provide an option to abort, the user has to know to run shutdown /a to terminate the process and potentially not. Ben's lack of the /f (force) option as long as the user has an application that requires saving of unsaved data, the system will be sitting at a prompt awaiting user response and will not shutdown. while other application might be terminated,.

implementing a policy that each person must shutdown their system and use monitoring tools to monitor system, and raise the issue with .. is a far better approach, what happens if the prompt for abort shows up while the person is away from their desk, pick your reason. The shutdown would then rely on the software used to have auto-save/recovery option.

IMHO, any remote/automated shutdown will generally over the time of the usage will cost more than the energy consumed by systems left on.
All it takes is for a single scenario of a high value project to be impacted by this automated shutdown over a 10 year period. i.e. pick your industry a proposal for a bid, an opening/closing statement, medical write up, etc. the person steps away from their desk, prompt pops up, a minute passes without a response, the shutdown triggered, all newly added data is lost. remote/unattended shutdown of PC is terminated the next day.

If unclear, IMHO it is an unwise policy to remotely shutdown system for energy saving consideration. Access related, GPO to enforce screen saver with a requirement for password is a more apt approach.  A person with physical access, has the access to power on a powered off system.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 1

Author Comment

by:Oscar Powers
ID: 42329348
Thanks Arnold for your answer, I need some time to think about it.
0
 
LVL 14
ID: 42329368
@Arnold  True, I forgot that the shutdown /a would be your method of stopping it, Silly!

It's late way too late, and I'm not thinking clearly (clearly ;) )
0
 
LVL 1

Author Comment

by:Oscar Powers
I'm using Right Click Tools to shutdown the computers.  This software allow me trough SCCM to  prompt users, they have the option to stop the shutdown if they still working.  The downside is I do not get a log of the process.  To check if the PCs are down I have to run a ping.
0

Featured Post

Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Join & Write a Comment

Sometimes clients can lose connectivity with the Lotus Notes Domino Server, but there's not always an obvious answer as to why it happens.   Read this article to follow one of the first experiences I had with Lotus Notes on a client's machine, my…
This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
The viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question