# Restart service/s on a remote server



## farooq_rcr (Sep 5, 2008)

Hi All,

I am looking at a script which can restart a microsoft service on a remote windows 2003 server. I should be able to execute the script from the comfort of my notebook, thus by entering all required information like username, password and domain name within the script.

Please let me know is there any script to do the above functionality. Thanks in advance.

Best Regds
Farooq


----------



## farooq_rcr (Sep 5, 2008)

Hi All,

I got the below script which is working fine on my local machine, but same functionality i want to have on a remote server, ie if i click from my desktop , i should be able to stop/start the service on a remote server. 

Option Explicit 
Dim objWMIService, objItem, objService 
Dim colListOfServices, strComputer, strService, intSleep 
strComputer = "computerName" 
intSleep = 15000 
WScript.Echo " Click OK, then wait " & intSleep & " milliseconds" 
'On Error Resume Next 
' NB strService is case sensitive. 
strService = " 'Alerter' " 
Set objWMIService = GetObject("winmgmts:" _ 
& "{impersonationLevel=impersonate}!\\" _ 
& strComputer & "\root\cimv2") 
Set colListOfServices = objWMIService.ExecQuery _ 
("Select * from Win32_Service Where Name ="_ 
& strService & " ") 
For Each objService in colListOfServices 
objService.StopService() 
WSCript.Sleep intSleep 
objService.StartService() 
Next 
WScript.Echo "Your "& strService & " service has Started" 
WScript.Quit 
' End of Example WMI script to Start / Stop services 


your help greatly appreciated.

Best Regds
Farooq


----------



## Krrose27 (Jul 7, 2008)

why not use rdc or vnc to performe this directly on the machine?


----------



## farooq_rcr (Sep 5, 2008)

Hi Krrose27,

I am a portal(SAP) consultant and i dont know anything about scripting :-(. Can you give some more details to implement the restart of particular service from my laptop.

Also with vnc i think we need to connect to server from the local machine and then perform the restart manually, my idea is to give this script to some one who do not understand anything about the restart of service, but his job is to run the file locally whenever some problem occurs, and restarting the service on remote server is a workaround for some issue what i am facing.

Let me know if you/any one has the solution for this.

Best Regds
Farooq


----------



## GraemeI (Aug 16, 2008)

Hey

You can use various tools to do this, best not to overcomplicate it I find. 
I use either CPAU or psservice (from sysinternals) You can just create a .bat using standard windows commands as well. 

have the command running from a batch file using psservice would work quite well for yourself. 
The command would be something like....

psservice \\server -u domain\username -p password stop service name

psservice \\server -u domain\username -p password start service name

Maybe this will help. Let me know if you want more info.


----------



## farooq_rcr (Sep 5, 2008)

Hi GraemeI,

Thanks a lot for your timely help, the batch file or direct command is working perfectly. Once again thanks a ton.

How ever, my service takes time to stop, may be some 10 seconds, i wanted to know whether i can provide any time delays between stop and start commands within a batch file.

Best Regds
Farooq


----------



## GraemeI (Aug 16, 2008)

You're very welcome. 

If you do not already have the resource kit tools download them as they are really useful linky and just use sleep N (n representing number of seconds) to delay whatever you want.


----------



## farooq_rcr (Sep 5, 2008)

Hi GraemeI,

I gave the command like this (in a batch file)

psservice \\serverName -u domain\username -p password stop Alerter

sleep 1500 (or psservice sleep 15)

psservice \\serverName -u domain\username -p password start Alerter 

but the sleep command is not working. Any help?

Best Regds
Farooq


----------



## farooq_rcr (Sep 5, 2008)

Hi Graeme,

Any update on how to make the delay working in below command(batch file).

If any one can let me know how to insert delay between stop and start of the service.(See my earlier post).

Thanks in advance

Best Regds
Farooq


----------



## GraemeI (Aug 16, 2008)

Hi Farooq,

Studying and working to much just now. 

Did you have any problems getting the resource kit tools for server 2003 from the link on my last post. 

If not what happens when you type sleep or sleep.exe into a command prompt from the machine you are running the script from?


----------

