Solved

.bat shutdown script

Posted on 2014-02-14
10
466 Views
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:

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
Comment
Question by:tike55
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 39

Expert Comment

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

>> %log%

that will append rather than overwrite.
0
 
LVL 20

Expert Comment

by:Lazarus
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
 Loop
 'close the text file
 oTS.Close


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

Expert Comment

by:Kyle Abrahams
ID: 39860460
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
 
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%
0
 

Author Comment

by:tike55
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?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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?


Steve
0
 

Author Comment

by:tike55
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.
0
 
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?
0
 
LVL 3

Expert Comment

by:ola_erik
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,
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
 
LVL 39

Accepted Solution

by:
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.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Short answer to this question: there is no effective WiFi manager in iOS devices as seen in Windows WiFi or Macbook OSx WiFi management, but this article will try and provide some amicable solutions to better suite your needs.
When it comes to security, there are always trade-offs between security and convenience/ease of administration. This article examines some of the main pros and cons of using key authentication vs password authentication for hosting an SFTP server.
After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…
After creating this article (http://www.experts-exchange.com/articles/23699/Setup-Mikrotik-routers-with-OSPF.html), I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now