Solved

Script to remotely restart service

Posted on 2009-05-06
6
659 Views
Last Modified: 2012-05-06
Hello,

I need to write a script that will allow me to remotely restart a service on a Windows 2003 server.  I've done things with net stop, net start etc before locally but wasn't sure what needed to be done to be able to run this from a workstation and restart the service on ServerXYZ.
0
Comment
Question by:mgthomas0
[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
6 Comments
 
LVL 17

Expert Comment

by:OriNetworks
ID: 24320362
You can use sc in a batch file or from a command prompt. I know this MS Article says windows 2000 and nt but the sc syntax is pretty much the same http://support.microsoft.com/kb/166819
0
 
LVL 17

Expert Comment

by:OriNetworks
ID: 24320394
sc \\remotecomputer start servicename

sc \\remotecomputer stop servicename
0
 
LVL 14

Accepted Solution

by:
rejoinder earned 400 total points
ID: 24320469
Here is a vbscript sample...
strComputer = "."
strService  = "Alerter"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServiceList = objWMIService.ExecQuery("SELECT * FROM Win32_Service WHERE Name='" & strService & "'")
 
Set WshShell = WScript.CreateObject("WScript.Shell")
 
For Each objService in colServiceList
    result = WshShell.Popup("Stopping: " & objService.DisplayName, 3)
    intResult = objService.StopService()
    if intResult <> 0 then
        wscript.echo "There was a problem stopping the service."
    end if
    WScript.Sleep 10000
    result = WshShell.Popup("Starting: " & objService.DisplayName, 3)
    intResult = objService.StartService()
    if intResult <> 0 then
        wscript.echo "There was a problem starting the service."
    end if
Next

Open in new window

0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 14

Expert Comment

by:rejoinder
ID: 24320487
Change the strComputer to the remote machine name and the strService to the service name you want to restart.
0
 
LVL 10

Expert Comment

by:TekServer
ID: 24320608
PSExec (part of the Sysinternals Suite) can be used to do this as well.

Example:

psexec \\%remotepc% -s net start %servicename%

Where "%remotepc% and %servicename% are variables in a batch file (or you can supply these explicitly, if you prefer; though with the variables and an input file you can feed the batch file a list of remotepc's and or services very quickly and efficiently).

HTH!
:)
0
 

Author Comment

by:mgthomas0
ID: 24320642
Thanks for the info!  I'll go through and test some of this out and get back to everyone, but it looks like this is exactly what I need.
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

691 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