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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With