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

Create .bat Auto Shutdown Script

I want to create a .bat script that will source a text file containing computer names on the network to shutdown.

I.E.

.bat file would be something like this:


"run following command on computers in text file:
c:\windows\system32\shutdown -s -f -t 60

and text file would be something like this:
workstaion 1
workstation 2
workstation 3

So, I would schedule the bat file to run every night on the company server, and it would shutdown all the computers at my work.  I have seen this done before, and it works great I just need some help with the syntax.
0
tike55
Asked:
tike55
  • 4
  • 2
1 Solution
 
Steve KnightIT ConsultancyCommented:
Something like this will loop through a file shutdowns.txt and run a shutdown command for each line.  Save it as "DoShutdowns.cmd" or something.

As it stands it logs the output so you should get a date/time logged entry for each computer.  If it fails to contact it then it should show an error in the log.

Steve

@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%

Open in new window


[edit]:  missed /f off the for command
0
 
tike55Author Commented:
thanks,

what would the syntax be for the text list (if any)
0
 
Steve KnightIT ConsultancyCommented:
Sorry, should have explained, Just one per line:

pcname
pcname2
pcname3

It could read values across one line etc. if wanted using different ways but one pc per line is more flexible IMO.  The for command reads each line and as it stands assigns everything upto the first space into %%a

You could probably use the "net view" as the source of your computer names but likely you will accidentally turn something else off by mistake, or it not including some computers with firewalls set differently etc.

If you can use the shutdown -s -m \\computer format then above should be good.

Steve
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
Steve KnightIT ConsultancyCommented:
Thanks, would appear accepting this answer tipped me over to being a "Wizard" in Miscellaneous networking too.... what a title!

You have achieved Wizard ranking in Miscellaneous Networking by earning 301,911 points!  To celebrate your new rank, we'd would like to send you a free Experts Exchange t-shirt.
0
 
tike55Author Commented:
Hi Steve,

the script doesn't shutdown the workstations when they are in sleep mode.,.

Also, it only seems like the last computer in the list gets put on the log file, no error shows up.
0
 
Steve KnightIT ConsultancyCommented:
Try this, I don't think it can wake up the sleeping pc 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%

Open in new window

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!

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