Solved

batch to shutdown windows services

Posted on 2013-12-09
6
424 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
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 250 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 69

Accepted Solution

by:
Qlemo earned 250 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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 69

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

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!

Question has a verified solution.

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

The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
A safe way to clean winsxs folder from your windows server 2008 R2 editions
This tutorial will walk an individual through the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…

825 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