"Status" return 的一个 WMI 功能是空的

"Status" return of a WMI feature is empty

我正在将安装程序从 VBS 转换为 C# 程序。 在此安装中,我必须使用 DISM 激活某些 windows 功能。

"cmd.exe", "/C Dism /Online /Enable-Feature /FeatureName:WAS-ProcessModel" 

我是这样激活的。当我用

手动检查它们时
dism /online /get-featureinfo /featurename:WAS-ProcessModel

在命令提示符下,我得到了功能的信息,包括状态。 (状态:已激活)

但是当我尝试通过我的程序获取它时,状态 return 只是空的。

这里是我程序的相关部分:

ManagementScope scope = new ManagementScope("\\.\ROOT\cimv2");

//create object query
ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_OptionalFeature Where Name=\"WAS-ProcessModel\"");

//create object searcher
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);

//get a collection of WMI objects
ManagementObjectCollection queryCollection = searcher.Get();

//enumerate the collection.
foreach (ManagementObject m in queryCollection)
{
    // access properties of the WMI object

    Console.WriteLine("Caption : {0}" + Environment.NewLine + "Status : {1}", m["Caption"], m["Status"]);

}

这个的return是:

Caption : Prozessmodell
Status : 

如何获取功能的状态? 我做错了什么吗?我是这个 DISM/WMI 的新手所以也许这只是我做错了一些基本的事情。

正如 documentation for the Status property on the Win32_OptionalFeature class 所说:

"This property is NULL."

您需要 InstallState 属性:

Identifies the state of the optional feature. The following states are possible:

Enabled (1)

Disabled (2)

Absent (3)

Unknown (4)

您可以将它们添加到枚举中,并使用它来显示输出:

public enum InstallState
{
    Enabled = 1,
    Disabled = 2,
    Absent = 3,
    Unknown = 4
}

foreach (ManagementObject m in queryCollection)
{ 
  var status = (InstallState)Enum.Parse(typeof(InstallState), m["InstallState"].ToString());

  Console.WriteLine("Caption : {0}" 
            + Environment.NewLine + "Status : {1}", m["Caption"], status);
}

然后returns:

Caption : Process Model

Status : Enabled