1

我有 C# 代码试图从 WMI 对象加载一些属性 - Win32_Service。但它返回给我 'System.Management.ManagementException: Invalid class' 错误。

我的简化代码:

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));
}

我还尝试通过 PowerShell 命令加载 WMI 对象列表 -

获取 WmiObject -List | 选择名称

它返回 91 个对象,但它错过了 Win32_Service 对象。我用谷歌搜索了如何重新安装它,但没有找到。有没有办法以某种方式重新安装它?

谢谢

更新 1: Powershell 命令的输出:

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
4

2 回答 2

1

尝试重新注册基类(当然是管理命令行......)

mofcomp %windir%\system32\wbem\cimwin32.mof
于 2013-08-27T14:01:34.823 回答
1

请参阅此处的“我收到 0x80041010(“无效类”)错误”部分。您可能会发现一些有用的东西,特别是ScriptomaticWbemtest的使用。

于 2013-08-26T07:01:19.320 回答