.bat shutdown script

Posted on 2014-02-14
Last Modified: 2014-04-02
I have a .bat script running on a server as a scheduled task to shutdown 2 workstations on the network every night.

the script is this:

@echo off
set list="c:\scripts\shutdowns.txt"
set log="c:\scripts\shutdownlog.txt"
for /f %%a in ('type %list%') do (
  shutdown -s -f -t 60 /m \\%%a
  ECHO %date%,%time%,%%a,%Errorleverl%
)> %log%

it sources the computer names on shutdowns.txt:


and produces a log file shutdownlog.txt:

Thu 02/13/2014,20:30:00.70,aa2-pc,

the problems with the script are:
1) log file only shows last PC on shutdowns.txt with no error message
2) both workstations did not shut down, probably due to the fact that they were in sleep mode.  this wasnt reflected in the log file as well.

If you could help me fix this that would be greatly appreciated!

Question by:tike55
  • 3
  • 3
  • 2
  • +2
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 39860434
> %log%
should be

>> %log%

that will append rather than overwrite.
LVL 20

Expert Comment

ID: 39860439
I run a VBS script that seems to work very well. You can use it to shut down all your network if you want it uses the "servershutdown.txt" file in the script with the names or IP's of the computers you want shutdown.

You might try it I might work better for you.

Set WSHShell = WScript.CreateObject("WScript.Shell")
 Set oFS = CreateObject("Scripting.FileSystemObject")

 strWarning = "Due to network maintenance, this computer must be shutdown. You have 5 minutes to save your work from the start of this countdown. Sorry for any inconvenience caused. "
 strDelay = 300 'Delay given in seconds; change this value to your preference, or set it to 0 to give no delay at all

 'Open a text file of computer names
 'with one computer name per line
 Set oTS = oFS.OpenTextFile("C:\Scripts\servershutdown.txt")

 'go through the text file
 Do Until oTS.AtEndOfStream
 'get the next computer name
 'store it in variable strCompname
 strCompname = oTS.ReadLine
 WshShell.Run "C:\Windows\System32\shutdown.exe -m \\" & strCompname & " -s -f -c " & Chr(34) & strWarning & Chr(34) & " -t " & strDelay 'Replace the "-r" switch with "-s" to make the computers shutdown instead
 'close the text file

EDIT: Sorry that didn't answer your question at all after re-reading. My apologies.
LVL 40

Expert Comment

by:Kyle Abrahams
ID: 39860460
Also from here:

you may need to authenticate yourself first.  (via the map).  They also have a power tools shutdown link available.
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

LVL 43

Expert Comment

by:Steve Knight
ID: 39860552
Actually it was intended to be this, the extra ( ) around the for command redirects the output in one go to log file.... for this many doesn't make much difference to using >> mind!

@echo off
set list="c:\scripts\shutdowns.txt"
set log="c:\scripts\shutdownlog.txt"
(for /f %%a in ('type %list%') do (
  shutdown -s -f -t 60 /m \\%%a
  ECHO %date%,%time%,%%a,%Errorleverl%
))> %log%

Author Comment

ID: 39860629
Hi Dragon-it since this is your script I'll ask ya,

1)  how can I get it to shutdown the PCs when they are in sleep mode?  PCs were on in the morning.

2)  also what does the 60 /m refer to?
LVL 43

Expert Comment

by:Steve Knight
ID: 39860738
1)  No idea sorry, will have to unsleep it first, not sure if that is possible from network - after all it won't be responding.  It may be possible to use something that will send a WOL (wake on lan) packet to the machine which would then need to be configred for WOL in BIOS or whatever that machine needs.  It would then have to boot, to then respond to shutdown command..  

2) the 60 goes with the -t , it means give it 60 seconds before starting shutdown. The -m is the option to tell it which computer.  Shutdown -? from a cmd.exe prompt will show you the options.

Maybe better, disabled "sleep" and set a scheduled job on the PC to run itself a shutdown command?


Author Comment

ID: 39860752
Last question then this is put to bed.  Do you think it will lower the lifespan of the HDs if I  disable sleep?

BTW, I will be adding alot of workstations to this script.
LVL 43

Expert Comment

by:Steve Knight
ID: 39861041
I wouldn't have thought it would make much difference, though the amount of electricity used will be more etc... what is the idea here, the machines stay on, you run stuff in the evening then shut them down?

Expert Comment

ID: 39861321
Hmm, If energy consumption and workstation longevity is your goal (and not eg. enforcing no usage) IMO You should consider setting a scheduled task for every workstation powering off after say 20:00 if idle.

This allows for more scenario flexibility (eg: one important user needs to work late just at one special time.)

Here's a suggestion: if idle after XX:YY poweroff, recheck every 30mins,

I don't remember at the top of my head how to push/export/manage scheduling events to all clients. Should be a well-lit road though.

(If not, last time I checked, all task resides as files in a directory, making it possible to create the task once and then copy it to that dir on all workstations. Ugly hack you shouldn't have to resort to though. Needs a bit of testing.)

for whats it worth,
LVL 40

Accepted Solution

Kyle Abrahams earned 500 total points
ID: 39866019
Just a note you can configure the power options to turn of the HDs while avoiding sleep.  (EG: turn off HDs after 30 minutes) or something.  This will keep the computers running while not allowing them to go to sleep and thereby be accessible.  As a side note I rarely shut down my home PC and have had 0 HD problems with it (was using a 10K RPM WD) . . . for about 9 years.

though ola_eriks link also provides some good suggestions.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to determine subnet mask? 11 40
Windows 7 Networking - Public vs. Work vs Public 8 37
svi stops eigrp advertisement 13 33
Autorun Remote Desktop - USB Stick 6 31
#Citrix #Citrix Netscaler #HTTP Compression #Load Balance
PRTG Network Monitor lets you monitor your bandwidth usage, so you know who is using up your bandwidth, and what they're using it for.
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

828 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