Solved

Restart services on remote server

Posted on 2011-03-01
12
685 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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
 
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Synchronize a new Active Directory domain with an existing Office 365 tenant
ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
The viewer will learn how to count occurrences of each item in an array.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

730 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