How to programatically check Windows services properties

May 29th, 2009 in Windows, vbscript. Add comment
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

You probably know how to check the properties of Windows services ( through Control Panel -> Administrative tools->Services). But sometimes you might need to check that programatically, especially if you are writing software that depends on whether a particular value has been set on a Window service. So how do you do that? Read on.

  • Fire up notepad and copy the following bit of VB script code
    strComputer = "."
     Set objWMIService = GetObject("winmgmts:" _
       & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
     Set colListOfServices = objWMIService.ExecQuery _
           ("Select * from Win32_Service")
     For Each objService in colListOfServices
      	WScript.Echo objService.StartMode
    
  • If you run the script as is you will be bombarded with a zillion popups showing the status of each service. So you will need to modify the select statement above to the service you want. To get the service name, go to Control Panel->Administrative Tools->Services and double click on the service of your choice. The service name will be shown under the general tab in the pop up window.
    For example, if I need to check only start up mode of the SNMP trap service, I will use the following select statement: Select * from Win32_Service Where Name=’SNMPTRAP’
  • Now save the script as .vbs file and run it (either from the command line or by double clicking on it).
  • The above script only checks the startup mode property of the services. Use any of the following as per your requirement in place of objService.StartMode :

    objService.AcceptPause
    objService.AcceptStop
    objService.Description
    objService.DesktopInteract
    objService.DisplayName
    objService.SystemName
    objService.Name
    objService.ServiceType
    objService.State
    objService.ExitCode
    objService.ProcessID
    objService.Caption
    objService.ErrorControl
    objService.PathName
    objService.Started
    objService.StartName

Related Post

How to execute a perl script from vbscript

Tags:

Leave a Reply

Sponsors