1

I am having a minor problem automating the starting and stopping of services. When I open services.msc and look at the list of services, they all have names. However, when I run this code:

Dim objService As Object
Dim objSet As Object
IsServiceRunning = False
Set objSet = GetObject("winmgmts:").ExecQuery("SELECT * FROM Win32_Service")
For Each objService In objSet
    If (UCase(strServiceName) = UCase(objService.Name)) And (UCase(objService.State) = UCase("Running")) Then
        IsServiceRunning = True
    End If
Next

The objService.Name value is not the same as the name in the list. For example, "Computer Browser" is just "browser", "Distributed File System" is "dfs", and "Net Logon" is "netlogon". Is there a way to pull the full, longer name for these services from this objService object? I can workaround this, but for the sake of clarity in the code I'd rather use the same value for determining if the service is running, making a NET START or NET STOP command line call, and logging.

Joe M
  • 3,060
  • 3
  • 40
  • 63
  • This looks like an WMI issue rather than anything specific to a programming language or to services. Keep in mind that WMI is based on a lot of hackish "by guess and by golly" spelunking through the registry and such, and doesn't always aggregate everything you want. You can probably do everything you need via API calls instead, even getting away from shelling runs of NET START and NET STOP. – Bob77 Jun 12 '12 at 23:28
  • For reference, you can see both the names in the service properties window. `Service name` is the internal name used by Windows and the SCM, and `Display name` is the user friendly name it shows. – Deanna Jun 13 '12 at 10:21

1 Answers1

3

Just use objService.Caption to access "long name" of service.

I discovered the name of the property like this:

For Each objService In objSet
    For Each vElem In objService.Properties_
        Debug.Print vElem.Name; "=";
        Debug.Print vElem.Value
    Next
    Exit For
    ...
Next

Just put objService in watch window to find out Properties_ property. Put vElem in watch window too to find Name and the default property Value (besides IsArray, etc.) of SWbemProperty object.

wqw
  • 11,771
  • 1
  • 33
  • 41