Win32_Service丢失了

时间:2013-08-23 17:39:34

标签: c# windows powershell wmi wmi-service

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

我的简化代码:

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对象列表 -

Get-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

2 个答案:

答案 0 :(得分:1)

请参阅“我收到0x80041010(”无效的类“)错误”here部分。您可能会发现一些有用的内容,尤其是使用ScriptomaticWbemtest

答案 1 :(得分:0)

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

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