We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Script to shutdown several Windows 2003 servers

Medium Priority
672 Views
Last Modified: 2012-05-06
Hi Experts.  Over the last couple of months, our server room has overheated 3 or 4 times to the point that the entire system had to be shutdown for a day.  While that is a problem in of itself, the other problem is that the servers were just powered off by using the "on/off" button on the servers, not shutdown gently.  This was mainly due to the IS Director coming in late at night or early, early morning and not knowing or wanting to shut them down one at a time.

So, with that said, I thought it would be easier on the servers and on the tech that had to come in to shutdown the servers to have a simple batch file that ran a script, supplied the reason (over heating) and then shut the servers down.  That all sounds great but my weakness is scripting.  I know of PSTools but I know that not all techs here have it installed so I would ask that PSTools (psshutdown) be used as a last resort.

Thanks Experts!
Comment
Watch Question

Speshalyst Tech Support professional
CERTIFIED EXPERT

Commented:
you could simply user SHUTDOWN -C and put the comment on it ..
 
Speshalyst Tech Support professional
CERTIFIED EXPERT

Commented:
check the usage of the Shutdown command for other switches that you might require
Commented:
Put in a bash the list of Server to reboot with the command:


shutdown /s /m \\victim    - Shuts down a remote machine called 'victim'

Optionally:
 /t for time.  Is 30 seconds too short (or too long) a time?   You can adjust with /t: 60 to display the dialog box one minute. Maximum is 600 seconds.

/f for force.  'Mr Nasty', is coming - ready or not!  Shutdown /r /f as you may have guessed, restarts the machine and closes any programs without warning.

/c for comment.  Would you like to put your stamp on the shutdown?  Let people know who is in charge, who is shutting them down?  /c " Guy is shutting you down"

/d p:4:1 reason.  Personally, I would avoid this switch, the syntax is tricky the numbers obscure and worst of all it does nothing exciting.  That said the idea is sound, it enters a reason for the shutdown in the event log.  If you activate shutdown's help, then you will get a whole list of major and minor reasons that you could employ with this switch.  For me, this switch is an option, an option that I do not take.

(example taken from computerperformance.co.uk )

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Robin HumanHead of Network Services
CERTIFIED EXPERT
Commented:
As speshalyst says, just use the "shutdown" command;
something like this (in a batch file):
echo off
cls
shutdown /f /m \\servername
shutdown /f /m \\servername2
shutdown /f /m \\servername3
shutdown /f /m \\servername4 /t 60
cls
print "servers shutdown due to overheating" > c:\Servers.txt

Commented:
forget this... The order of switches is important!

shutdown /m /s \\machine      - does not work.

Author

Commented:
Thanks for the posts and suggestions.  From what I gather, there are two recommendations here.  @pghn, please specify what your recommendation is as I got lost by the "forget this"...  ; )  

#1
echo off
cls
shutdown /f /m \\servername
shutdown /f /m \\servername2
shutdown /f /m \\servername3
shutdown /f /m \\servername4 /t 60
cls
print "servers shutdown due to overheating" > c:\Servers.txt

#2
pghn will clarify

I will test #1 on a test box now.  Thanks for the help.

Commented:
Just saying that forget to mention that the the order of commands its important.

Commented:
save the script with vbs extension

the script read computers or servers list from txt file( line by line)

change name and path of the txt file in this line:

Set objlist = objfso.OpenTextFile("c:\list.txt", ForReading)
const forreading = 1
Set objfso = CreateObject("scripting.filesystemobject")
set objShell = CreateObject("wscript.Shell")
 
Set objlist = objfso.OpenTextFile("c:\list.txt", ForReading)
 
Do Until objlog.AtEndOfStream
strComputer = objlog.ReadLine
objShell.Run "shutdown -s -t 0 -f -m \\" & strComputer
Loop

Open in new window

Author

Commented:
Oh....  ; )  Thanks for clarifying that.  Thought you were going in a different route but understand you were adding detail to the previous experts post.  

Author

Commented:
@ yehudaha- >  In this line ,

Set objlist = objfso.OpenTextFile("c:\list.txt", ForReading)

can I specify the server and share name of a place this file could reside?  ie, \\server\share\list.txt

Commented:
:-)

no new vbscript post i think will help you and do the job more easily

Commented:
yes no problem

Commented:
yes no problem about your question about the share

Author

Commented:
Great!  That gives me two options to test.  If I have any questions, I will post them here.

Thanks again for everyone's help.

Author

Commented:
I'm guessing there should be a close or exit at the end of this?

echo off
cls
shutdown /f /m \\servername
shutdown /f /m \\servername2
shutdown /f /m \\servername3
shutdown /f /m \\servername4 /t 60
cls
print "servers shutdown due to overheating" > c:\Servers.txt
<<<<???>>>>

Author

Commented:
Hmmmm.....  I'm just getting the command window with a blink cursor.  What am I missing?  Here is the script that I am using:


echo off
cls
shutdown /f /m \\jeffcoattny /t 60
cls
print "servers shutdown due to overheating" > c:\Servers.txt
exit

Author

Commented:
There is no servers.txt on the C: drive.  ; )

Author

Commented:
@yehudaha-  Here is the error message I get when I run the vb script
vbs.error.bmp
Robin HumanHead of Network Services
CERTIFIED EXPERT

Commented:
There is no exit command on the batch file - it will automatically close when it has run;
instead of print, put Echo

Author

Commented:
Ohh.....  Guess my noob scripting skills have reared their ugly head.  Thanks RobinHuman.

Commented:
opps change this:

Do Until objlog.AtEndOfStream
strComputer = objlog.ReadLine

to

Do Until objlist.AtEndOfStream
strComputer = objlist.ReadLine

Author

Commented:
How long should it take before it reboots?  I have it still up after 1+ minutes.

Commented:
you mean shutdown,

maybe the server you talking about is stuck
Robin HumanHead of Network Services
CERTIFIED EXPERT

Commented:
It should start shutting down after 30 seconds, but depending on what is running on it, it may take a while to actually shut down.
You can monitor whether it is responding by using the following command from a dos window:
ping {servername} -t
the -t bit means it will continue pinging until you tell it to stop by pressing CTRL + C

Author

Commented:
: )  Yes, shutdown.  I ran a constant ping (-t) on the IP but it never went down.  I remoted in and restarted it.  Before I tested the scripts again, I wanted to confirm that everything in the script looked right.  Thanks, I will resume testing.

Author

Commented:
Robin, I tried running your script several times but no success.  I noticed that there was no -s switch included and once I used that, it worked.

shutdown -s -f -m \\servername
echo "Server is about to melt" > c:\DR_Log.txt

Author

Commented:
@yehudaha:  Help me with this as I am not sure why I am getting this error.  I know there is something I did but I have no idea what.


const forreading = 1
Set objfso = CreateObject("scripting.filesystemobject")
set objShell = CreateObject("wscript.Shell")
 
Set objlist = objfso.OpenTextFile("c:\%userprofile%\desktop\server.tools\list.txt", ForReading)
 
Do Until objlist.AtEndOfStream
strComputer = objlist.ReadLine
objShell.Run "shutdown -s -t 0 -f -m \\" & strComputer
Loop

Open in new window

Author

Commented:
Strike that, here is the script I used and got an error.
const forreading = 1
Set objfso = CreateObject("scripting.filesystemobject")
set objShell = CreateObject("wscript.Shell")
 
Set objlist = objfso.OpenTextFile("%userprofile%\desktop\server.tools\list.txt", ForReading)
 
Do Until objlist.AtEndOfStream
strComputer = objlist.ReadLine
objShell.Run "shutdown -s -t 0 -f -m \\" & strComputer
Loop

Open in new window

error01.bmp
Robin HumanHead of Network Services
CERTIFIED EXPERT
Commented:
You're right - sorry;
we run this to restart, so have the -r switch set - I took that out but forgot to put in the -s...
:-)

Author

Commented:
No problem.  Glad you provided a built-in test for me.  : )

Commented:
hey the script you used by me

in vbscript %userprofile% exist only with addon code

so please write another path or the full path to the txt file

Author

Commented:
Well, that would be a problem as I am not versed enough to do this.  I will need you to recommend a way to use a variable like that or let me know if you can't so I can accept the other expert's post.  Thanks!  

Commented:
here
const forreading = 1
Set objfso = CreateObject("scripting.filesystemobject")
set objShell = CreateObject("wscript.Shell")
Set UserProfile = objshell.ExpandEnvironmentStrings("%userprofile%")
 
Set objlist = objfso.OpenTextFile(UserProfile & "\desktop\server.tools\list.txt", ForReading)
 
Do Until objlist.AtEndOfStream
strComputer = objlist.ReadLine
objShell.Run "shutdown -s -t 0 -f -m \\" & strComputer
Loop

Open in new window

Author

Commented:
Closer....


error2.bmp
Commented:
sh...

who put the set over there

fix
const forreading = 1
Set objfso = CreateObject("scripting.filesystemobject")
set objShell = CreateObject("wscript.Shell")
UserProfile = objshell.ExpandEnvironmentStrings("%userprofile%")
 
Set objlist = objfso.OpenTextFile(UserProfile & "\desktop\server.tools\list.txt", ForReading)
 
Do Until objlist.AtEndOfStream
strComputer = objlist.ReadLine
objShell.Run "shutdown -s -t 0 -f -m \\" & strComputer
Loop

Open in new window

Author

Commented:
Hahahahaha!!  Thanks.  Testing now.
Holy cow you guys make this so difficult. Here dude, make a list of the servers you want to reboot and save it as SERVERS.TXT, then paste the following code into whatever name you want (e.g. HOTBOXES.CMD), and run it.

for /f %%A in (servers.txt) do set var_current=%%A&call :proc_logandkill
goto :eof
:proc_logandkill
eventcreate /s \\%var_current% /id 407 /t warning /l system /d "The system was shutdown due to bad air conditioning."
shutdown -m \\%var_current% -f -s
goto :eof

It will create an event in the system event log of the server, and then force it to shutdown after 60 seconds.

Author

Commented:
@Justin , LOL!!  Thanks for the script.  I will test it out and then close up this question.  Thanks for all of the help experts!
Robin HumanHead of Network Services
CERTIFIED EXPERT

Commented:
No problem!

Author

Commented:
@yehudaha:  That works now!

@Justin:  Working on your suggestion now.

Author

Commented:
Justin, it works as advertised.  Very good.  I am figuring up the points now and will close this out.  Thanks again for the help.

Author

Commented:
Great work experts.  I have three scripts to choose from now that they have been tested and shown to work as desired.  I appreciate your time and assistance with this.  Take care!

Regards,
Aaron

Commented:
for /f %%A in (servers.txt) do set var_current=%%A&call :proc_logandkill
goto :eof
:proc_logandkill
eventcreate /s \\%var_current% /id 407 /t warning /l system /d "The system was shutdown due to bad air conditioning."
shutdown -m \\%var_current% -f -s
goto :eof

This above script Justin posted works EXCELLENT.  but we would like to force the shutdown immediately and not wait the 30 seconds.  The above script runs the default 30 seconds then shuts down.  Can anyone assist in eliminating the 30 seconds and make it 0 seconds?

Thanks!

Author

Commented:
shutdown -m \\%var_current% -f -s -t 0

The -t 0 is the syntax for time (-t) and the 0 is how long to wait.  I'm pretty sure 30 is the default if -t isn't used.

Commented:
Thank you!  One last question.  We want to run this from a domain workstation that might not have domain access to shutdown the servers.  
I want to be able to add domain authentication into the script so it can be run from anywhere we need.  

What are the tags to add a username and password for our domain?

Thanks again!  Your tip worked.  
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.