Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to RESTART a service on a remote computer?

Status
Not open for further replies.

markdmac

MIS
Dec 20, 2003
12,340
US
Anybody have an example of a script to restart services on a remote computer? I am thinking I probably need to use WMI to do it right.

I know I could use a call statment to run a Net Stop command, problem then is getting it to start up again as soon as possible since there isn't a Net Restart command.

Last point, I am hoping to use the code within ASP.

Any help would be appreciated.

I hope you find this post helpful. Please let me know if it was.

Regards,

Mark
 
net stop "some service" & net start "some service"


that will work if you can execute net.exe remotely....the '&' sign tells the net start to execute after the net stop no matter if net stop was successful or not. You could also do :

net stop "some service" && net start "some service"

which means net start will only execute if net stop was successful...



This is also very helpful when you need to do something like this remotely, because you know as soon as you execute 'ipconfig /release' you lose connectivity...

ipconfig /release && ipconfig /renew


^
| isnt that great....HTH mike
 
Thanks Stiddy. I managed to get this all working with two ASP pages. One stops the services and has a button to restart them. Clicking on the restart button brings you to a second page that reports if the services were started or not and gives a button to stop them again if needed.


You && trick will certainly come in handy some day however.

Thanks.

I hope you find this post helpful. Please let me know if it was.

Regards,

Mark
 
i would avoid the WMI way and opt for a computer object instead, not sure if that is the right word for it.

'########### handle the services
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set objComputer = GetObject("WinNT://" & WshNetwork.ComputerName & ",computer")

Set objService = objComputer.GetObject("service", "clisvc")
If objService.Status = 4 Then
objService.Stop
End If
 
This script may help. It uses WMI but this should not be a problem on Win2k or newer machines.

'///////////////////////////////////////////////////////
'/////////// User Defined Values //////////////////
'///////////////////////////////////////////////////////

'Change this value to the name of the service you want to restart. i.e. This one restarts the task scheduler service.
Const SERVNAME = "Schedule"

'Amount of time to wait before restarting the service in milliseconds. i.e. This is 10 seconds.
Const WAITTIME = 10000

'set to dot(.) for the local machine or machine name for a remote machine
strComputer = "."

'/////////////// End User Defined Values /////////////


Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colServices = objWMIService.ExecQuery ("SELECT * FROM win32_Service")
For Each objService in colServices
'WScript.Echo objService.Name 'Uncomment to list all service names on the machine
If objService.Name = SERVNAME Then
errReturnCode = objService.StopService()
WScript.Sleep WAITTIME
errReturnCode = objService.StartService()
End If
Next

'End of Script
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top