Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Script to remotely restart service

Posted on 2009-05-06
6
Medium Priority
?
664 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

604 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