Link to home
Start Free TrialLog in
Avatar of Oscar Powers
Oscar PowersFlag for United States of America

asked on

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
        }
    }
}
SOLUTION
Avatar of Ben Personick (Previously QCubed)
Ben Personick (Previously QCubed)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Oscar Powers

ASKER

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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Arnold for your answer, I need some time to think about it.
@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 ;) )
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.
I think the solution is to have a local PowerShell  script and run it with a Task Scheduler.
Hey Oscar,

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