Solved

batch to shutdown windows services

Posted on 2013-12-09
6
435 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 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 70

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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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 70

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
For anyone that has accidentally used newSID with Server 2008 R2 (like I did) and hasn't been able to get the server running again because you were unlucky (as I was) and had no backups - I was able to get things working by doing a Registry Hive rec…
This tutorial will show how to push an installation of Backup Exec to an additional server in both 2012 and 2014 versions of the software. Click on the Backup Exec button in the upper left corner. From here, select Installation and Licensing, then I…
This tutorial will give a short introduction and overview of Backup Exec 2012 and how to navigate and perform basic functions. Click on the Backup Exec button in the upper left corner. From here, are global settings for the application such as conne…
Suggested Courses

630 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