Solved

batch to shutdown windows services

Posted on 2013-12-09
6
418 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 68

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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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 68

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
You might have come across a situation when you have Exchange 2013 server in two different sites (Production and DR). After adding the Database copy in ECP console it displays Database copy status unknown for the DR exchange server. Issue is strange…
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…
This tutorial will walk an individual through setting the global and backup job media overwrite and protection periods in Backup Exec 2012. Log onto the Backup Exec Central Administration Server. Examine the services. If all or most of them are stop…

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now