Win32_Service is missing

3.6k views Asked by At

I have C# code that tries to load some property from WMI object - Win32_Service. But it returns to me 'System.Management.ManagementException: Invalid class ' error.

My simplified code:

static void Main(string[] args)
{
    string serviceName = "AppFabricEventCollectionService";
    string propertyName = "StartName";
    var obj = GetProperty(serviceName, propertyName);
}

private static string GetProperty(string serviceName, string propertyName)
{
    using (ManagementObject obj2 = GetWindowsServiceManagementObject(serviceName))
    {
        return (obj2.GetPropertyValue(propertyName) as string);
    }
}
public static ManagementObject GetWindowsServiceManagementObject(string serviceName)
{
    return new ManagementObject(string.Format("Win32_Service.Name='{0}'", serviceName));
}

Also I've tried to load list of WMI objects by PowerShell command -

Get-WmiObject -List | Select Name

It returns 91 objects, but it misses Win32_Service object. I googled how to reinstall it, but didn't found it. Is there a way to reinstall it somehow?

Thank you

Update 1: Output for Powershell command:

PS C:\Windows\system32> Get-WmiObject Win32_Service
Get-WmiObject : Invalid class "Win32_Service"
At line:1 char:1
+ Get-WmiObject Win32_Service
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidType: (:) [Get-WmiObject], ManagementExce
   ption
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.C
   ommands.GetWmiObjectCommand
2

There are 2 answers

1
David Brabant On

See the section "I’m getting an 0x80041010 (“Invalid Class”) error" here. You might find something useful, in particular the use of Scriptomatic and Wbemtest.

1
Martin Binder On

Try reregistering the base classes (admin commandline, of course...)

mofcomp %windir%\system32\wbem\cimwin32.mof