none
How to have cancel shut down pop up in shut down script

    Question

  • Good morning.

    I have a script that I have found on the web and modified it for my needs to a point and the point is I am now stuck.

    What I am trying to do with this script is that when run from a management server at a set time it will shut down computers in a OU.

    What I need in this script is a prompt to appear on the users desktop whether that are logged in that your Computer is going to

    shut down and if you wish to continue to work press cancel shutdown button which will run shutdown /a in the background for the user and cancel the shutdown.

    The script is as follows.

    '****************************************************************************

    OPTION Explicit
    DIM cn,cmd,rs
    DIM objRoot
    DIM strRoot, strFilter, strScope

    ' *******************************************************************
    ' * Setup
    ' *******************************************************************
    ' Specify OU of computers you want to shutdown
    strRoot = "OU=IT-Services,OU=Test-Staging-Area,DC=test,DC=test"
    strFilter = "(objectCategory=Computer)"
    ' Search child organizational units.  Use "onelevel" to search only the specified OU.
    strScope = "subtree"
    ' *******************************************************************

    set cmd = createobject("ADODB.Command")
    set cn = createobject("ADODB.Connection")
    set rs = createobject("ADODB.Recordset")

    cn.open "Provider=ADsDSOObject;"
    cmd.activeconnection = cn

    cmd.commandtext = "<LDAP://" & strRoot & ">;" & strFilter & ";" & _
              "name;" & strScope
    '**** Bypass 1000 record limitation ****
    cmd.properties("page size")=1000

    set rs = cmd.execute

    while rs.eof <> true and rs.bof <> true
        ShutDownComputer(rs("name"))

        rs.movenext
    wend

    cn.close

    ' Subroutine to shutdown a computer
    private sub ShutDownComputer(byval strComputer)
        dim strShutDown,objShell
        
        ' -s = shutdown, -t 60 = 60 second timeout, -f = force programs to close
        strShutdown = "shutdown.exe -s -f -t 3600 -m \\" & strComputer

            set objShell = CreateObject("WScript.Shell")

            objShell.Run strShutdown, 0, false

    end sub

    '****************************************************************************

    Any help in adding this to the script will be much appreciated.

    Thanks

    Neil



    • Edited by Neilwr Wednesday, May 23, 2012 2:48 AM
    Wednesday, May 23, 2012 1:59 AM

Answers

  • This issue come up frequently in this and other forums.

    There are many third party solutions that allow for local interaction with a user and a granular control of a shutdown.

    The issue of a user being away or the users that load a small script that constantly sends a few keystrokes into a dummy window will defeat this.

    Most companies just define a policy that once a month all workstations will be restarted after maintenance.  The timing will be distributed to reduce load and users will be notified of what their schedule will be.  Most of the time this is the most acceptable and manageable method. There will always be a few exceptions - usually managers - which can be handled through Group Policy security and filters.

    SInce XP the need to restart systems regularly has been eliminated except when certain legacy programs are used.  In my domains we have workstations that have not been restarted for many months.  We seldom see a need to  do this.  SOme older database software like DOS FoxPro and programs built using it may induce memory leaks that cannot be cleared until the users is logged off.  Some legacy kernel mode print drivers can also force a machine to need rebooting. Once we have eliminated the print drivers most of the need to reboot disappears.

    I am very much in favor of fixing the problems that cause the systems to need restarting.  This will always improve the reliability of your systems.


    ¯\_(ツ)_/¯

    Wednesday, May 23, 2012 12:26 PM

All replies

  • Dear 'Stuck'.  How is it. "Stuck' is a global phenomenon.

    Consider the following: "What I need in this script is a prompt to appear on the users desktop whether that are logged in or logged off ...."

    How in the world can you send a message to a user that is logged off..?????

    I suspect that you missed the fact that April first has come and gone.  Sometimes even the best jokes lack a usable punch line!


    ¯\_(ツ)_/¯


    • Edited by jrv Wednesday, May 23, 2012 3:21 AM
    Wednesday, May 23, 2012 2:18 AM
  • The problem with your current approach is that you are remotely shutting down the computers, if you want a prompt to appear on a users desktop you would have to execute something locally on the machine. What are you trying to achieve by rebooting the computers?

    Jaap Brasser
    http://www.jaapbrasser.com

    Wednesday, May 23, 2012 6:36 AM
    Moderator
  • I am trying to achieve a nightly shut down of our computers with a prompt for the user to cancel if they are still working.
    Wednesday, May 23, 2012 8:12 AM
  • I am trying to achieve a nightly shut down of our computers with a prompt for the user to cancel if they are still working.

    You need to keep these points in mind:

    • What you are trying to do is not straightforward.
    • As with all complex solutions, there is a risk of things going wrong.
    • The potential damage of an inappropriate shutdown is large, e.g. when the chief accountant loses the annual report for the next day's shareholder meeting. You'll probably get the sack.
    • The benefit of shutting down PCs automatically seems rather small.

    A cost/benefit analysis suggests that this is a high-risk low-benefit project.

    Wednesday, May 23, 2012 8:56 AM
  • Okay, the problem with this is that people that keep their workstations logged in at night with documents open will still lose their data. Also when someone walks away for a minute to get a cup of coffee their workstation could be rebooted as well. I am not saying that this is wrong, just something to consider before you implement this.

    To circumvent this you could do one of the following:

    1. Build a check to see if any user is logged on and do not initiate shutdown if anyone is logged in.
    2. Execute a script locally the presents the user with a prompt that allow the user to cancel the shutdown. You could use PSEXEC, WinRM or PowerShell to achieve this
    3. You could add a message with the shutdown command informing the user how to cancel the shutdown (easiest to implement)
    shutdown -r -c "If you wish to cancel this shutdown, please click the Cancel Shutdown icon on your desktop" -t 90


    Jaap Brasser
    http://www.jaapbrasser.com

    Wednesday, May 23, 2012 9:12 AM
    Moderator
  • This issue come up frequently in this and other forums.

    There are many third party solutions that allow for local interaction with a user and a granular control of a shutdown.

    The issue of a user being away or the users that load a small script that constantly sends a few keystrokes into a dummy window will defeat this.

    Most companies just define a policy that once a month all workstations will be restarted after maintenance.  The timing will be distributed to reduce load and users will be notified of what their schedule will be.  Most of the time this is the most acceptable and manageable method. There will always be a few exceptions - usually managers - which can be handled through Group Policy security and filters.

    SInce XP the need to restart systems regularly has been eliminated except when certain legacy programs are used.  In my domains we have workstations that have not been restarted for many months.  We seldom see a need to  do this.  SOme older database software like DOS FoxPro and programs built using it may induce memory leaks that cannot be cleared until the users is logged off.  Some legacy kernel mode print drivers can also force a machine to need rebooting. Once we have eliminated the print drivers most of the need to reboot disappears.

    I am very much in favor of fixing the problems that cause the systems to need restarting.  This will always improve the reliability of your systems.


    ¯\_(ツ)_/¯

    Wednesday, May 23, 2012 12:26 PM
  • This is another case where the OP bails becuse of the complexity of the problem.  I guess sthis is also why ther are numerous third party solutions for this.  Some add nice bells and whistles at a real sweet price level - for the vendor.

    Tis has been an ongoing issue with WIndows for years.  I suspect that Microsoft would so well to add a prompted shutdown/restart but it is harder to do in scripting.  We can do it but it should really not be necessary - but then I like machines that obey!


    ¯\_(ツ)_/¯

    Wednesday, May 30, 2012 1:24 AM
  • JRV I did not bail but working hard to find a solution None of the post or replies solved my issue.

    I also have to say you were no help.

    Here is the script i came up with.  Decided to run it locally.

    cmd /c shutdown -s -t 600
    write-host "Press any key to abort reboot"
    $key = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    cmd /c shutdown -a

    You should have vote for not helpful instead of vote as helpful as you are not.

    Wednesday, May 30, 2012 3:20 AM
  • JRV I did not bail but working hard to find a solution None of the post or replies solved my issue.

    I also have to say you were no help.

    Here is the script i came up with.  Decided to run it locally.

    cmd /c shutdown -s -t 600
    write-host "Press any key to abort reboot"
    $key = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    cmd /c shutdown -a

    You should have vote for not helpful instead of vote as helpful as you are not.

    Sorry but that is not what it appeared you were asking for.  Forgive my skepticism but it was hard to understand all of the code you were trying to post and what it had to do with the request.

    You don't say how you are running this to get it to work.  It will only work if the user is an admin and runs it locally.  This does not appear to be what you asked for although I could be not understanding what you wanted.

    If this completely solves your problem then good.


    ¯\_(ツ)_/¯

    Wednesday, May 30, 2012 3:31 AM
  • JRV I did not bail but working hard to find a solution None of the post or replies solved my issue.

    I also have to say you were no help.

    Here is the script i came up with.  Decided to run it locally.

    cmd /c shutdown -s -t 600
    write-host "Press any key to abort reboot"
    $key = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    cmd /c shutdown -a

    You should have vote for not helpful instead of vote as helpful as you are not.

    If you want to do this remotely, you can try PSShutDown.exe from SysInternals.  It has options that allow it to be run remotely AND allow the user to cancel, among many other options.

    Rich Prescott | Infrastructure Architect, Windows Engineer and PowerShell blogger | MCITP, MCTS, MCP

    Engineering Efficiency
    @Rich_Prescott
    Windows System Administration tool
    AD User Creation tool

    Wednesday, May 30, 2012 3:46 AM
    Moderator
  • You can also write a smalla pplication whin will intercept the shutdown message and can cancel the shutdown.  Thisis the same way that PsShutdown works,


    ¯\_(ツ)_/¯

    Wednesday, May 30, 2012 5:54 PM