Solved

Restart services on remote server

Posted on 2011-03-01
12
686 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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
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

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

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

The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

696 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