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

batch to shutdown windows services

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
rutgermons
Asked:
rutgermons
2 Solutions
 
Chris MillardCommented:
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
 
Brett DanneyIT ArchitectCommented:
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
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
gurutcCommented:
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
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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
 
gurutcCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

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