Solved

a script to run Shutdown command for a list of computers.

Posted on 2004-10-26
304 Views
Last Modified: 2010-04-10
I would like to run Shutdown.exe command for a list of computers. so I would like to have a script that does this task, and then I will schedule this script to run at certain time.
Thanks
0
Question by:Chuckbuchan
    14 Comments
     
    LVL 9

    Expert Comment

    by:TannerMan
    Visit www.sysinternals.com
    Excellent source for remote command tools
    0
     
    LVL 10

    Expert Comment

    by:NetworkArchitek
    Hi Chuckbuchan,
    This should work, I haven't tested it though.  First please make a text file called Computers.txt and put it in the same directory as this script, which you should name shutdown.vbs. The Computers.txt file will contain the names of your computers on each line, like:

    Comp1
    Comp2

    And so forth. Here is the script, if you look at the "shutdown \\" line you can change switches, I just put "/y /c" You may want to change that or leave it.


    Set WshShell=WScript.CreateObject("WScript.Shell")
    INPUT_FILE_NAME = "Computers.txt"
    Const FOR_READING = 1
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile(INPUT_FILE_NAME, FOR_READING)
    strComputers = objFile.ReadAll
    objFile.Close
    arrComputers = Split(strComputers, vbCrLf)
    For Each strComputer In arrComputers
           WshShell.Run("shutdown \\" & strComputer & " " & "/y /c")
    Next


    Just save that as shutdown.vbs, let me know if any problems arise. Oh yeah, and of course WMI must be running correctly on this systems for this to work.


    Cheers!
    0
     

    Author Comment

    by:Chuckbuchan
    I haven't tried the script yet.
    Is anyone on the network who runs this script can make the specified computer(s) shut down?
    0
     

    Author Comment

    by:Chuckbuchan
    I just come to try the script it didn't shutdown any of the computers.
    0
     
    LVL 2

    Expert Comment

    by:UnifiedIT
    If these are windows 2000 or XP computers copy this and paste it into a notepad, name the file ShutComp.bat (be sure to save as all files)

    shutdown -m \\computer1 /s /f /d p:0:0 /t 60 -c "This computer is shutting down,please save your work"
    shutdown -m \\computer2 /s /f /d p:0:0 /t 60 -c "This computer is shutting down,please save your work"
    etc..

    You can then put it in your c:\Windows\System32 folder and run it from the Start, Run command by typing ShutComp, and just as easily create a task to do it for you.

    -m will shutdown a remote computer
    /s tells it to shutdown
    /f tells it to force all apps to close
    /d is a reason code for shutdown (if these are windows 2003 servers, you need it)
    /t sets a 60 second time before shutdown
    -c lets you leave a message
    0
     
    LVL 2

    Expert Comment

    by:UnifiedIT
    You can put in either the computer name or ip address after the \\ also..
    0
     
    LVL 10

    Expert Comment

    by:NetworkArchitek
    Chuck, well you have to have the correct shutdown.exe for starters. Of course you make a bat file and spell out the command for each computer ever time you want to do it but this is not a very elegant solution. This way you simply have a centralized text file which you simply pop in and out computer names, you can even automate that process.
    0
     

    Author Comment

    by:Chuckbuchan
    to UnifiedIT:
    I tried your batch file and worked just perfect, but since users know the name of our servers maybe the IPs, so they can shutdown our servers if anyone of them know those commands. is there a way to  prevent that?
    thanks
    0
     
    LVL 2

    Expert Comment

    by:UnifiedIT
    NetArch, I know your solution is cooler, I just gave a simple answer (plus I am not proficient at vb scripting, so I go simple)

    Chuck,

    The shutdown.exe command must be performed by someone with proper rights to shutdown the server. (IE A local admin on your server)
    Just make sure that only local admins and those that you specify are part of the Administrators group on your servers, I wouldnt have "Authenticated Users, or Domain users" in any of the groups though..

    If you Admin an Active Directory domain, you can remove the users rights to command prompt through group policies also.
    0
     
    LVL 10

    Expert Comment

    by:NetworkArchitek
    UnifiedIT is right in that you must have proper rights. Also, I just tested the script and it works flawlessly, assuming you have rights.
    0
     

    Author Comment

    by:Chuckbuchan
    to UnifiedIT: do you know where it's located the GPO setting about disabling Command Prompt for users?
    thanks
    0
     

    Author Comment

    by:Chuckbuchan
    to UnifiedIT : some applications that run on workstations needs to be closed properly before the windows shutdown.
    Is there any switch to add to the shutdown command to close applications properly(regardless of the saving option)  before it forces the shutdown.
    thanks
    0
     
    LVL 2

    Accepted Solution

    by:
    Chuck,

    The GPO stuff should be asked in another question, but I have the info here and ill answer it quick.
    This is not the ONLY way, but it will do the job for you.
    User Configuration/Administrative Templates/System
         - Dont Run Specified Windows Applications
         - Enable this and put cmd.exe and command.exe here

    the /f option forces all applications to close, I do not know if this is proper or not?
    If you have apps that need to be closed a certain way, I would test this before implementing it.
    0
     
    LVL 2

    Expert Comment

    by:UnifiedIT
    Hey NetworkArchitek,

    Thanks for posting that script, I will store that away for a rainy day.
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    I was recently sitting at a desk at work with one of my colleagues and needed some information on my home computer. He watched as I turned on my home computer, established a remote session into it, got the information I needed and then shut it down …
    Article by: IanTh
    Hi Guys After a whole weekend getting wake on lan over the internet working, I thought I would share the experience. Your firewall has to have a port forward for port 9 udp to your local broadcast x.x.x.255 but if that doesnt work, do it to a …
    Viewers will learn how to connect to a wireless network using the network security key. They will also learn how to access the IP address and DNS server for connections that must be done manually. After setting up a router, find the network security…
    After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…

    931 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now