Solved

Restart services on remote server

Posted on 2011-03-01
12
680 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Of course you can set thsi batch up as a scheduled task if need be.  Old School, but it works.
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 7

Assisted Solution

by:wparrott
wparrott earned 167 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

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

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
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 …

763 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

6 Experts available now in Live!

Get 1:1 Help Now