?
Solved

Script to shutdown several Windows 2003 servers

Posted on 2009-02-19
46
Medium Priority
?
659 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!
0
Comment
Question by:samiam41
  • 23
  • 9
  • 5
  • +4
46 Comments
 
LVL 16

Expert Comment

by:speshalyst
ID: 23682246
you could simply user SHUTDOWN -C and put the comment on it ..
 
0
 
LVL 16

Expert Comment

by:speshalyst
ID: 23682260
check the usage of the Shutdown command for other switches that you might require
0
 
LVL 5

Assisted Solution

by:pghn
pghn earned 100 total points
ID: 23682420
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 12

Assisted Solution

by:RobinHuman
RobinHuman earned 400 total points
ID: 23682436
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
 
LVL 5

Expert Comment

by:pghn
ID: 23682446
forget this... The order of switches is important!

shutdown /m /s \\machine      - does not work.
0
 
LVL 9

Author Comment

by:samiam41
ID: 23682581
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
 
LVL 5

Expert Comment

by:pghn
ID: 23682794
Just saying that forget to mention that the the order of commands its important.
0
 
LVL 14

Expert Comment

by:yehudaha
ID: 23682822
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
 
LVL 9

Author Comment

by:samiam41
ID: 23682823
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
 
LVL 9

Author Comment

by:samiam41
ID: 23682886
@ 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
 
LVL 14

Expert Comment

by:yehudaha
ID: 23682894
:-)

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

Expert Comment

by:yehudaha
ID: 23682916
yes no problem
0
 
LVL 14

Expert Comment

by:yehudaha
ID: 23682926
yes no problem about your question about the share
0
 
LVL 9

Author Comment

by:samiam41
ID: 23683040
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
 
LVL 9

Author Comment

by:samiam41
ID: 23684031
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
 
LVL 9

Author Comment

by:samiam41
ID: 23684061
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
 
LVL 9

Author Comment

by:samiam41
ID: 23684073
There is no servers.txt on the C: drive.  ; )
0
 
LVL 9

Author Comment

by:samiam41
ID: 23684249
@yehudaha-  Here is the error message I get when I run the vb script
vbs.error.bmp
0
 
LVL 12

Expert Comment

by:RobinHuman
ID: 23685459
There is no exit command on the batch file - it will automatically close when it has run;
instead of print, put Echo
0
 
LVL 9

Author Comment

by:samiam41
ID: 23685481
Ohh.....  Guess my noob scripting skills have reared their ugly head.  Thanks RobinHuman.
0
 
LVL 14

Expert Comment

by:yehudaha
ID: 23686528
opps change this:

Do Until objlog.AtEndOfStream
strComputer = objlog.ReadLine

to

Do Until objlist.AtEndOfStream
strComputer = objlist.ReadLine
0
 
LVL 9

Author Comment

by:samiam41
ID: 23686634
How long should it take before it reboots?  I have it still up after 1+ minutes.
0
 
LVL 14

Expert Comment

by:yehudaha
ID: 23686827
you mean shutdown,

maybe the server you talking about is stuck
0
 
LVL 12

Expert Comment

by:RobinHuman
ID: 23687641
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
 
LVL 9

Author Comment

by:samiam41
ID: 23692373
: )  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
 
LVL 9

Author Comment

by:samiam41
ID: 23710995
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
 
LVL 9

Author Comment

by:samiam41
ID: 23711946
@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
 
LVL 9

Author Comment

by:samiam41
ID: 23711973
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
 
LVL 12

Assisted Solution

by:RobinHuman
RobinHuman earned 400 total points
ID: 23711983
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
 
LVL 9

Author Comment

by:samiam41
ID: 23712259
No problem.  Glad you provided a built-in test for me.  : )
0
 
LVL 14

Expert Comment

by:yehudaha
ID: 23712568
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
 
LVL 9

Author Comment

by:samiam41
ID: 23712997
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
 
LVL 14

Expert Comment

by:yehudaha
ID: 23713251
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
 
LVL 9

Author Comment

by:samiam41
ID: 23713327
Closer....


error2.bmp
0
 
LVL 14

Assisted Solution

by:yehudaha
yehudaha earned 500 total points
ID: 23715434
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
 
LVL 9

Author Comment

by:samiam41
ID: 23726679
Hahahahaha!!  Thanks.  Testing now.
0
 
LVL 6

Accepted Solution

by:
Justin_W_Chandler earned 1000 total points
ID: 23750568
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
 
LVL 6

Expert Comment

by:Justin_W_Chandler
ID: 23750579
It will create an event in the system event log of the server, and then force it to shutdown after 60 seconds.
0
 
LVL 9

Author Comment

by:samiam41
ID: 23766009
@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
 
LVL 12

Expert Comment

by:RobinHuman
ID: 23767881
No problem!
0
 
LVL 9

Author Comment

by:samiam41
ID: 23774207
@yehudaha:  That works now!

@Justin:  Working on your suggestion now.
0
 
LVL 9

Author Comment

by:samiam41
ID: 23774469
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
 
LVL 9

Author Closing Comment

by:samiam41
ID: 31548790
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
 

Expert Comment

by:Plote
ID: 34012490
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
 
LVL 9

Author Comment

by:samiam41
ID: 34013035
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
 

Expert Comment

by:Plote
ID: 34013190
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

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month13 days, 8 hours left to enroll

749 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