Solved

Restart services on remote server

Posted on 2011-03-01
12
681 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
  • 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
 
LVL 9

Accepted Solution

by:
tl121000 earned 167 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 7

Assisted Solution

by:wparrott
wparrott earned 167 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 166 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

863 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

29 Experts available now in Live!

Get 1:1 Help Now