Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: High
  • Security: Public
  • Views: 62
  • Last Modified:

Turn Off remote computers with PowerShell

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
Oscar Powers
Asked:
Oscar Powers
  • 5
  • 3
2 Solutions
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
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
 
Oscar PowersAuthor Commented:
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
 
arnoldCommented:
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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
Oscar PowersAuthor Commented:
Thanks Arnold for your answer, I need some time to think about it.
0
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
@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
 
Oscar PowersAuthor Commented:
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
 
Oscar PowersAuthor Commented:
I think the solution is to have a local PowerShell  script and run it with a Task Scheduler.
0
 
Ben Personick (Previously QCubed)Lead Network EngineerCommented:
Hey Oscar,

  Glad I was able to help you out on this one :)
0
 
Oscar PowersAuthor Commented:
Thanks for your time and help.
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now