3

I am converting a Installer Programm from VBS into a C# programm. In this installation i have to activate some windows features with DISM.

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

I activated them in this way. And when i check them manualy with

dism /online /get-featureinfo /featurename:WAS-ProcessModel

in the command prompt, then i get the information of the Feature, including the Status. (Status : Activated)

But when i try to get it via my programm the Status return is just empty.

Here the relevant part of my programm:

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"]);

}

The return of this is:

Caption : Prozessmodell
Status : 

How can i get the status of the feature? Am i doing something completely wrong? I am new to this DISM/ WMI things so maybe it is just some basic thing i did wrong.

stuartd
  • 70,509
  • 14
  • 132
  • 163
TomK
  • 33
  • 5

1 Answers1

3

As the documentation for the Status property on the Win32_OptionalFeature class says:

"This property is NULL."

You need the InstallState property instead:

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

Enabled (1)

Disabled (2)

Absent (3)

Unknown (4)

You can add those to an enum, and use that to display the output:

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

This then returns:

Caption : Process Model

Status : Enabled

stuartd
  • 70,509
  • 14
  • 132
  • 163