?
Solved

Script to remotely restart service

Posted on 2009-05-06
6
Medium Priority
?
661 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 1600 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

Question has a verified solution.

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

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

762 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