?
Solved

Restart services on remote server

Posted on 2011-03-01
12
Medium Priority
?
687 Views
Last Modified: 2012-05-11
Hi

I have five Windows 2003 SP2 servers running an application named HR1. The server names are App1, App2, App3, App4, App5. This app has about four services that it runs under per server, all named the same - Service1, Service2, Service3, Service4.

Sometimes, we need to restart three of these services after maintenance.

We do this by RDP'ing to App1 > Computer Manager > Services

Restarting those three services, then "connect to another machine" and restarting those services etc

As you can imagine, this is quite time consuming.

I would like to create a script that will do this automatically from my PC.

Does any know how I can do this? Is Powershell applicable here? Or do we need to use something else?
0
Comment
Question by:tomd1976
[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
  • 4
  • 3
  • 3
  • +2
12 Comments
 
LVL 11

Expert Comment

by:Akenathon
ID: 35010867
This little utility PsService is the solution for you: http://technet.microsoft.com/en-us/sysinternals/bb897542

Check out the other utilities, for instance PsExec for remote execution of arbitrary commands, etc.
0
 
LVL 27

Expert Comment

by:KenMcF
ID: 35010893
You can do this using WMI and powershell

$Servers = "App1", "App2", "App3", "App4", "App5"
$Services "Service1", "Service2", "Service3"

Foreach ($Server in $Servers){
$Services | %{
$Service = gwmi -computer -class win32_service | where {$_.Name -eq $($_)}
$Service.stopservice()
$Service.startservice()
}
}
0
 

Author Comment

by:tomd1976
ID: 35010899
Hi thanks

Appreciate that - do you know how I would do this via a script too?
0
Four New Appliances. Same Industry-leading Speeds.

But don't take it from us.  The Firebox M370 is Miercom tested and Miercom approved, outperforming its competitors for stateless and stateful traffic throughput scenarios.  Learn more about the M370, M470, M570 and M670 and find the right solution for your organization today!

 
LVL 9

Accepted Solution

by:
tl121000 earned 668 total points
ID: 35010974
For each server app1 through 5 create a .bat (batch file)
***************************
sc \\app1 stop "service1"
sc \\app1 stop "service2"
sc \\app1 stop "service3"
sc \\app1 stop "service4"
sc \\app1 start "service1"
sc \\app1 start "service2"
sc \\app1 start "service3"
sc \\app1 start "service4"

add app2 and services, app3, and so on
0
 
LVL 9

Expert Comment

by:tl121000
ID: 35010987
One batch file with all 5 servers and a stop and start for each services, so you click it once.

One and done
0
 
LVL 9

Expert Comment

by:tl121000
ID: 35010993
Of course you can set thsi batch up as a scheduled task if need be.  Old School, but it works.
0
 
LVL 7

Assisted Solution

by:wparrott
wparrott earned 668 total points
ID: 35011017
Easiest way that I have found to do something similar is to get the PsTools suite from MS:

http://technet.microsoft.com/en-us/sysinternals/bb897542

then write a batch script that runs the PsService tool:

psservice [\\computer [-u username] [-p password]] <command> <options>

So in your case something like this:

psservice \\App1 -u admin -p ****** restart Service1

for %%I in (App1 App2 App3 App4 App5) do (

c:\temp\psservice \\%%I -u admin -p ******* restart Service1
c:\temp\psservice \\%%I -u admin -p ******* restart Service2
c:\temp\psservice \\%%I -u admin -p ******* restart Service3
c:\temp\psservice \\%%I -u admin -p ******* restart Service4

)

Open in new window

0
 

Author Comment

by:tomd1976
ID: 35011114
Thanks, tl121000's solution looks the most straightforward:

sc \\app1 stop "service1"
sc \\app1 stop "service2"
sc \\app1 stop "service3"
sc \\app1 stop "service4"
sc \\app1 start "service1"
sc \\app1 start "service2"
sc \\app1 start "service3"
sc \\app1 start "service4"

Could you tell me what sc stands for?

Also, is there any way to get 'feedback' on whether the service stopped and started ok? Would this be reported?

0
 
LVL 11

Expert Comment

by:Akenathon
ID: 35012021
SC stands for "Service Control". You can use "sc query" to check the status of a service, see here for syntax and examples:

http://ss64.com/nt/sc.html

http://commandwindows.com/sc.htm
0
 

Author Comment

by:tomd1976
ID: 35021382
Ok, great, was just wondering if it was a requirement or not?

Also, is there any way to get 'feedback' on whether the service stopped and started ok? Would this be reported
0
 
LVL 11

Assisted Solution

by:Akenathon
Akenathon earned 664 total points
ID: 35037984
You do get feedback whenever you send any command using "sc". The catch is, if the service is stopped and you say e.g.:

sc \\app1 start "W32Time"

Your feedback will read something like this:

SERVICE_NAME: W32Time
        TYPE               : 20  WIN32_SHARE_PROCESS
        STATE              : 2  START_PENDING
                                (NOT_STOPPABLE,NOT_PAUSABLE,IGNORES_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x7d0
        PID                : 1976
        FLAGS              :

You need to pause for a couple of seconds, and then use sc query to confirm that the startup went fine:

C:\Documents and Settings\babate>sc \\app1 query "W32Time"

SERVICE_NAME: W32Time
        TYPE               : 20  WIN32_SHARE_PROCESS
        STATE              : 4  RUNNING
                                (STOPPABLE,NOT_PAUSABLE,ACCEPTS_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0
0
 
LVL 9

Expert Comment

by:tl121000
ID: 35038134
I would like to add a modification stop the servives first for all Servers then start

sc \\app1 stop "service1"
sc \\app1 stop "service2"
sc \\app1 stop "service3"
sc \\app1 stop "service4"
sc \\app2 stop "service1"
sc \\app2 stop "service2"
sc \\app2 stop "service3"
sc \\app2 stop "service4"
sc \\app3 stop "service1"
sc \\app3 stop "service2"
sc \\app3 stop "service3"
sc \\app3 stop "service4"
.....
....
sc \\app1 start "service1"
sc \\app1 start "service2"
sc \\app1 start "service3"
sc \\app1 start "service4"
sc \\app2 start "service1"
sc \\app2 start "service2"
sc \\app2 start "service3"
sc \\app2 start "service4"
sc \\app3 start "service1"
sc \\app3 start "service2"
sc \\app3 start "service3"
sc \\app3 start "service4"

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Suggested Courses

764 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