Solved

Script to remotely restart service

Posted on 2009-05-06
6
648 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
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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

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

You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

707 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

19 Experts available now in Live!

Get 1:1 Help Now