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

.bat shutdown script

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:

aa-pc
aa2-pc


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!

thanks
0
tike55
Asked:
tike55
  • 3
  • 3
  • 2
  • +2
1 Solution
 
Kyle AbrahamsSenior .Net DeveloperCommented:
> %log%
should be

>> %log%

that will append rather than overwrite.
0
 
LazarusCommented:
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
 Loop
 'close the text file
 oTS.Close


EDIT: Sorry that didn't answer your question at all after re-reading. My apologies.
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
Also from here:
http://www.raymond.cc/blog/how-to-remotely-turn-off-computer-from-lan-or-wan/2/


you may need to authenticate yourself first.  (via the map).  They also have a power tools shutdown link available.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Steve KnightIT ConsultancyCommented:
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%
0
 
tike55Author Commented:
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?
0
 
Steve KnightIT ConsultancyCommented:
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?


Steve
0
 
tike55Author Commented:
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.
0
 
Steve KnightIT ConsultancyCommented:
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?
0
 
ola_erikCommented:
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,
http://www.howtogeek.com/howto/30758/make-your-pc-shut-down-at-night-but-only-when-youre-not-using-it/

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,
cheers
0
 
Kyle AbrahamsSenior .Net DeveloperCommented:
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.
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 3
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now