I've been writing a program in C# to scavenge information out of WMI as a project (I know many great tools out there exist already, but this is both for learning and custom tailored to an environment I manage), and I've come across a few oddities. Currently tripping me up is the following snippet below:
'using System.Management;
ManagementClass managementClass = new ManagementClass(new ManagementPath(@"\\" + pHostName + @"\root\" + pNameSpace));
EnumerationOptions enumOptions = new EnumerationOptions();
enumOptions.EnumerateDeep = false;
foreach(ManagementObject wmiClass in managementClass.GetSubclasses())
{
Console.WriteLine(wmiClass["__Class"].ToString());
}'
Given the definition of System.Management.ManagementClass under both the MSDN documentation and the VS Documentation, which I'll paraphrase pretty heavily to say it represents a CIM class, and the definition for the GetSubclasses() method which returns a collection of all subclasses under a class (under a class being the key phrase there), the only logical conclusion I can make as to why this works is that a Namespace in WMI is actually an instance of a class, which sounds incredibly wrong.
Are Namespaces actually instances of classes in WMI? If not, does anyone know why using a ManagementClass on a namespace allows class scavenging? The language used is really confusing me.
EDIT: Going to answer the question here. Hans Passant below pointed out that IWBEMPath is the cause of the issue, and that Namespaces are treated as both directories and Classes by ManagementPath due to IWbemPath, which is used to instantiate a new instance of ManagementClass. Thanks Hans!