locked
WMI Relationship between Class, Alias, and Namespace RRS feed

  • Question

  • I'm trying to make something to give me system information such as CPU & memory usage but I can't find any explanation of exactly what Windows Management Instrumentation is. The description in TechNet is extremely long-winded and unhelpful so I'm hoping someone can just answer this for me:

    When I enumerate all namespaces and their classes on my PC, why do I not see the alias CPU mentioned which is mapped to "SELECT * FROM WIN32_PROCESSOR" ?

    As far as I can tell, there are several different way to talk about WMI but clear way on how to work with it and it's getting quite frustrating.

    Code most copied from the MSDN. I also have no idea how ListClasses knows which namespace it's supposed to be iterating but somehow it does.

            public string ListNamespaces()
            {
                StringBuilder sb = new StringBuilder();
                int count = 0;
                try
                {
                    // Enumerate all WMI instances of 
                    // __namespace WMI class.
                    ManagementClass nsClass =
                        new ManagementClass(
                        new ManagementScope("root"),
                        new ManagementPath("__namespace"),
                        null);
                    foreach (ManagementObject ns in
                        nsClass.GetInstances())
                    {
                        sb.AppendLine((ns["Name"].ToString()));
                        sb.AppendLine(ListClasses());
                        count++;
                    }
                    sb.AppendLine();
                    sb.AppendLine(count.ToString() + " namespaces found.");
                }
                catch (ManagementException e)
                {
                    var a = e.Message;
                }
                return sb.ToString();
            }
            public string ListClasses()
            {
                StringBuilder sb = new StringBuilder();
                int count = 0;
                try
                {
                    // Perform WMI object query on 
                    // selected namespace.
                    ManagementObjectSearcher searcher =
                        new ManagementObjectSearcher(
                        new ManagementScope(
                        "root/default"),
                        new WqlObjectQuery(
                        "select * from meta_class"),
                        null);
                    foreach (ManagementClass wmiClass in
                        searcher.Get())
                    {
                        sb.AppendLine("\t" + wmiClass["__CLASS"].ToString());
                        count++;
                    }
                }
                catch (ManagementException e)
                {
                    var a = e.Message;
                }
                return sb.ToString();
            }
    EDIT:/ I'm particularly confused on why sometimes WMI queries are shown with Object.Property model, others they're some sort of weird LDAP query, and other times they're a SQL statement.


    • Edited by Luke_UK Thursday, August 6, 2015 4:51 PM
    Thursday, August 6, 2015 4:45 PM

Answers

  • Hi,

    Here I found a good explanation about WMI Namespace, Providers, and Classes. Please take a look.

    WMI provides a model for how to represent, store and query configuration and status information and other operational aspects of Windows computers. Developers can use WMI to write scripts or managed code to view and modify configuration settings on Windows computers, to view the status of Windows applications and services, and do a whole lot of other things useful to administrators who deploy, maintain and troubleshoot Windows-based networks.

    In a short summary, WMI stands for:

    • Windows - works on and for computers running Microsoft Windows 
    • Management - can be used to manage these computers 
    • Instrumentation - provides instruments for viewing and modifying what goes on under the hood with these computers

    WMI Namespaces

    Just about anything--well, not quite. Actually finding out whether WMI can do something in particular takes a bit of patience and understanding. Let's start by considering WMI namespaces. In WMI terminology, a namespace is a kind of logical database of classes and their instances.

    Set objWMIService = GetObject("winmgmts:\\.\root")
    Set colNamespaces = objWMIService.InstancesOf("__NAMESPACE")

    WMI Providers

    Finding the right namespace is just the first challenge (though much of the time connecting to the default namespace will suffice). You also have to access the right provider in order to retrieve or update data on the system you are targeting.

    WMI Classes

    Besides namespaces and providers, you also need to understand WMI classes if you want to leverage the power of WMI to script Windows administration tasks. A class is a kind of template for a type of object you can manage using WMI. For example, the class named Win32_LogicalDisk is a template for logical disks on Windows machines, and WMI uses this class to generate one instance of Win32_LogicalDisk for each installed disk.

    All above are referring article "Managing Windows Networks Using Scripts - Part 3: Understanding WMI", For more details, please search use these keywords. 

    Best regards,

    Kristin



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Friday, August 7, 2015 6:32 AM
  • Thanks. It seems that I'll have to hunt through each of the classes to find the information I'm after as there is no clear design in the product, it's a complete mash up of everything that is accessed in different ways through too many different providers and no one knows how it works.
    Friday, August 7, 2015 9:53 AM

All replies

  • Hi,

    Here I found a good explanation about WMI Namespace, Providers, and Classes. Please take a look.

    WMI provides a model for how to represent, store and query configuration and status information and other operational aspects of Windows computers. Developers can use WMI to write scripts or managed code to view and modify configuration settings on Windows computers, to view the status of Windows applications and services, and do a whole lot of other things useful to administrators who deploy, maintain and troubleshoot Windows-based networks.

    In a short summary, WMI stands for:

    • Windows - works on and for computers running Microsoft Windows 
    • Management - can be used to manage these computers 
    • Instrumentation - provides instruments for viewing and modifying what goes on under the hood with these computers

    WMI Namespaces

    Just about anything--well, not quite. Actually finding out whether WMI can do something in particular takes a bit of patience and understanding. Let's start by considering WMI namespaces. In WMI terminology, a namespace is a kind of logical database of classes and their instances.

    Set objWMIService = GetObject("winmgmts:\\.\root")
    Set colNamespaces = objWMIService.InstancesOf("__NAMESPACE")

    WMI Providers

    Finding the right namespace is just the first challenge (though much of the time connecting to the default namespace will suffice). You also have to access the right provider in order to retrieve or update data on the system you are targeting.

    WMI Classes

    Besides namespaces and providers, you also need to understand WMI classes if you want to leverage the power of WMI to script Windows administration tasks. A class is a kind of template for a type of object you can manage using WMI. For example, the class named Win32_LogicalDisk is a template for logical disks on Windows machines, and WMI uses this class to generate one instance of Win32_LogicalDisk for each installed disk.

    All above are referring article "Managing Windows Networks Using Scripts - Part 3: Understanding WMI", For more details, please search use these keywords. 

    Best regards,

    Kristin



    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.



    Friday, August 7, 2015 6:32 AM
  • Thanks. It seems that I'll have to hunt through each of the classes to find the information I'm after as there is no clear design in the product, it's a complete mash up of everything that is accessed in different ways through too many different providers and no one knows how it works.
    Friday, August 7, 2015 9:53 AM
  • Thanks. It seems that I'll have to hunt through each of the classes to find the information I'm after as there is no clear design in the product, it's a complete mash up of everything that is accessed in different ways through too many different providers and no one knows how it works.
    Yes, I agree with you.

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, August 10, 2015 8:12 AM