Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

batch to shutdown windows services

Posted on 2013-12-09
6
Medium Priority
?
453 Views
Last Modified: 2013-12-20
Folks

How can I create a batch that can logon to 2 or 3 different servers which invoke a command to shutdown 5 windows servers on each server ? in addition, I would like to specify the stop order in the batch but also ensure between stopping each service that there is a 60 second wait period between stopping the next

all help will do
0
Comment
Question by:rutgermons
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 17

Expert Comment

by:Chris Millard
ID: 39705665
I would use the free PSEXEC command (part of PSTools) to remotely execute the commands:-

http://technet.microsoft.com/en-gb/sysinternals/bb897553.aspx

PsExec can be used to run any commands on remote servers. The only thing you might have issue with is the waiting 60 seconds. Although I believe the Windows 2003 resource kit (again a free download) has a sleep.exe command that can be used to pause a script:-

http://www.microsoft.com/en-gb/download/details.aspx?id=17657

OR this free alternative-

http://www.sleepcmd.com/
0
 
LVL 13

Assisted Solution

by:SagiEDoc
SagiEDoc earned 1000 total points
ID: 39705835
The command is simple.
shutdown -m \\servername -s -f

shutdown /? will give you the switches (there are additional settings like aborting and so on)  but in the above -m = remote system, -s = shutdown, -f = force.

Download the Windows Server Resource kit (if you do not have it) here: http://www.microsoft.com/en-za/download/details.aspx?id=17657

In there is a utility called sleep.exe, this will pause your script which will look something like this:

shutdown -m \\server1 -s -f
sleep 180
shutdown -m \\server2 -s -f
sleep 180
shutdown -m \\server3 -s -f

Sleep 180 makes the script stop for 180 seconds.
0
 
LVL 71

Accepted Solution

by:
Qlemo earned 1000 total points
ID: 39705837
PsExec has some issues with network drives and scripts, but else it is a good idea to use it. You can also use a common trick to wait for some time.
Create the following batch file on the machine running the task (I'll call it c:\Scripts\StopServices.cmd):
@echo off
for %%S in ("Service 1", "Service 2", "Service 3") do (
  net stop %%S
  >nul ping -n 61 127.0.0.1
)

Open in new window

Create the file containing the servers to run the script on (I'll call it C:\Scripts\Servers.txt):
server1
server2
server3

Open in new window

Than run it with PsExec:
psexec @C:\Scripts\Servers.txt -c c:\Scripts\StopServices.cmd -v -s -d

Open in new window

This combination will
run the commands without waiting for completion (-d,
applying them to all listed servers (@...) at the same time,
copying (-c ...) the script to the remote System32 folder, but only if newer or not existing (-v),
using the remote System account because of -s
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 16

Expert Comment

by:gurutc
ID: 39705887
Hi, all the above is good, but you will also have to add the option to psexec to accept the eula if you run it within a batch file and point it to other servers.  Just start the command for psexec this way:

psexec /accepteula

Open in new window

 

and add the addtional commands and options as needed.

example using other expert's excellent post:

psexec /accepteula @C:\Scripts\Servers.txt -c c:\Scripts\StopServices.cmd -v -s -d

Open in new window


Good Luck,
- gurutc
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 39705909
You only have to accept the EULA once on the machine running psexec, so that isn't a big issue, but yes, it is a good idea to always add that switch.
0
 
LVL 16

Expert Comment

by:gurutc
ID: 39705932
We've seen it happen when pointing to other servers.  psexec actually temporarily installs itself on the remote system as a service and then executes the remote commands.  Since the remote system's never run psexec it fails silently because of the eula.

It's one of those 'sometimes it works, sometimes it don't' things.

- gurutc
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

A safe way to clean winsxs folder from your windows server 2008 R2 editions
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
This tutorial will walk an individual through configuring a drive on a Windows Server 2008 to perform shadow copies in order to quickly recover deleted files and folders. Click on Start and then select Computer to view the available drives on the se…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

705 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