Solved

Script to remotely restart service

Posted on 2009-05-06
6
654 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA & VBSCRIPT: re-adapt log file 13 33
randomize data from excell cell 15 45
Copy 20 files at a time from drive G to drive H 10 48
Collapse and expand table in Word 2010 2 31
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

839 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