views:

30

answers:

2

I Just want to manage Windows Services through Scripting languages( VB Script or Something ). Like Starting, stopping, Getting the status, Checking the dependencies etc. Please help with Code snippets or URL referances.

+2  A: 

Use this script to start a service:

'' Starts service 'strService' on computer named 'strComputer'
''
Sub StartService(strService, strComputer)
    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.StartService
    Next
End Sub

'' Start Windows CardSpace service on local host:
StartService "idsvc", "."

Instead of StartService, you can use StopService to stop the service. See this MSDN article for other useful methods of Win32_Service.

fmunkert
This helped me lot. Thanks. But what to do if there are dependency services? will that too stop and start along ? also is there any "RestartService" like start and stop service ? Please help. Thanks in Advance.
Vijay Athreyan
Please see the documentation of the StartService method (http://msdn.microsoft.com/en-us/library/aa393660(VS.85).aspx). Any services on which the started service depends will be started automatically; any services that depend on the started service will not be started automatically. If you want to do the latter, you might use one of the script samples from http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/services/ (look for `Win32_DependentService`).
fmunkert
A: 

I think fmunkert's script is worth to try.

Hi and welcome to Stack Overflow. Please note that this is a Q/A site and not a forum. So please don't post comments as answers. If you don't yet have enough [reputation](http://meta.stackoverflow.com/q/7237/131247) to add comments to posts, [you](http://meta.stackoverflow.com/q/17204/131247) [can](http://meta.stackoverflow.com/q/58006/131247) [gain](http://meta.stackoverflow.com/q/21463/131247) it by contributing to the site.
Helen
S. I am using his code only. Thanks
Vijay Athreyan