• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 663
  • Last Modified:

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!
0
samiam41
Asked:
samiam41
  • 23
  • 9
  • 5
  • +4
5 Solutions
 
speshalystCommented:
you could simply user SHUTDOWN -C and put the comment on it ..
 
0
 
speshalystCommented:
check the usage of the Shutdown command for other switches that you might require
0
 
pghnCommented:
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 )
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
RobinHumanCommented:
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
0
 
pghnCommented:
forget this... The order of switches is important!

shutdown /m /s \\machine      - does not work.
0
 
samiam41Author 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.
0
 
pghnCommented:
Just saying that forget to mention that the the order of commands its important.
0
 
yehudahaCommented:
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

0
 
samiam41Author 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.  
0
 
samiam41Author 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
0
 
yehudahaCommented:
:-)

no new vbscript post i think will help you and do the job more easily
0
 
yehudahaCommented:
yes no problem
0
 
yehudahaCommented:
yes no problem about your question about the share
0
 
samiam41Author 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.
0
 
samiam41Author 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
<<<<???>>>>
0
 
samiam41Author 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
0
 
samiam41Author Commented:
There is no servers.txt on the C: drive.  ; )
0
 
samiam41Author Commented:
@yehudaha-  Here is the error message I get when I run the vb script
vbs.error.bmp
0
 
RobinHumanCommented:
There is no exit command on the batch file - it will automatically close when it has run;
instead of print, put Echo
0
 
samiam41Author Commented:
Ohh.....  Guess my noob scripting skills have reared their ugly head.  Thanks RobinHuman.
0
 
yehudahaCommented:
opps change this:

Do Until objlog.AtEndOfStream
strComputer = objlog.ReadLine

to

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

maybe the server you talking about is stuck
0
 
RobinHumanCommented:
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
0
 
samiam41Author 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.
0
 
samiam41Author 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
0
 
samiam41Author 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

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

0
 
samiam41Author Commented:
Closer....


error2.bmp
0
 
yehudahaCommented:
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

0
 
samiam41Author Commented:
Hahahahaha!!  Thanks.  Testing now.
0
 
Justin_W_ChandlerCommented:
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

0
 
Justin_W_ChandlerCommented:
It will create an event in the system event log of the server, and then force it to shutdown after 60 seconds.
0
 
samiam41Author 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!
0
 
RobinHumanCommented:
No problem!
0
 
samiam41Author Commented:
@yehudaha:  That works now!

@Justin:  Working on your suggestion now.
0
 
samiam41Author 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.
0
 
samiam41Author 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
0
 
PloteCommented:
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!
0
 
samiam41Author 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.
0
 
PloteCommented:
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.  
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 23
  • 9
  • 5
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now