Link to home
Start Free TrialLog in
Avatar of samiam41
samiam41Flag for United States of America

asked on

Script to shutdown several Windows 2003 servers

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!
Avatar of Speshalyst
Speshalyst
Flag of India image

you could simply user SHUTDOWN -C and put the comment on it ..
 
check the usage of the Shutdown command for other switches that you might require
SOLUTION
Avatar of pghn
pghn
Flag of Portugal 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
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
forget this... The order of switches is important!

shutdown /m /s \\machine      - does not work.
Avatar of samiam41

ASKER

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.
Just saying that forget to mention that the the order of commands its important.
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

Oh....  ; )  Thanks for clarifying that.  Thought you were going in a different route but understand you were adding detail to the previous experts post.  
@ 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
:-)

no new vbscript post i think will help you and do the job more easily
yes no problem
yes no problem about your question about the share
Great!  That gives me two options to test.  If I have any questions, I will post them here.

Thanks again for everyone's help.
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
<<<<???>>>>
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
There is no servers.txt on the C: drive.  ; )
@yehudaha-  Here is the error message I get when I run the vb script
vbs.error.bmp
There is no exit command on the batch file - it will automatically close when it has run;
instead of print, put Echo
Ohh.....  Guess my noob scripting skills have reared their ugly head.  Thanks RobinHuman.
opps change this:

Do Until objlog.AtEndOfStream
strComputer = objlog.ReadLine

to

Do Until objlist.AtEndOfStream
strComputer = objlist.ReadLine
How long should it take before it reboots?  I have it still up after 1+ minutes.
you mean shutdown,

maybe the server you talking about is stuck
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
: )  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.
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
@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

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
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
No problem.  Glad you provided a built-in test for me.  : )
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
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!  
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

Closer....


error2.bmp
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
Hahahahaha!!  Thanks.  Testing now.
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
It will create an event in the system event log of the server, and then force it to shutdown after 60 seconds.
@Justin , LOL!!  Thanks for the script.  I will test it out and then close up this question.  Thanks for all of the help experts!
No problem!
@yehudaha:  That works now!

@Justin:  Working on your suggestion now.
Justin, it works as advertised.  Very good.  I am figuring up the points now and will close this out.  Thanks again for the help.
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
Avatar of Plote
Plote

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!
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.
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.