none
constructing WMI query in ManagementObjectSearcher RRS feed

  • Question

  • Hi,

    I've got the following query which returns me a list of results:

     var list = new List<Tuple<object, object>>();
                
                var scope = new ManagementScope(@"\root\CIMV2", null);
    
                scope.Connect();
                ObjectQuery query = new ObjectQuery("SELECT * FROM Win32_PnPDevice");
                ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
                foreach (ManagementObject obj in searcher.Get())
                {
                    list.Add(new Tuple<object, object>(obj["SameElement"],obj["SystemElement"]));
                }
                return list;

    It returns 2 properties which MSDN defines as:

    CIM_LogicalDevice REF SameElement;
    Win32_PnPEntity   REF SystemElement;

    How can i incorporate into my query so i can recieve the a property called "DeviceID" from the SystemElement property and is it possible to be done in the single call?

    Thursday, June 2, 2016 5:43 AM

Answers

  • Hi Milsnips,

    The WMI Query Language (WQL) is a subset of ANSI SQL — with some semantic changes. Not everything you can do in SQL is possible in WQL. To get the DeviceID, we could use following code.

    foreach (ManagementObject obj in searcher.Get())
    {
        var deviceID = new ManagementObject(obj["SystemElement"].ToString()).GetPropertyValue("DeviceID");
        list.Add(new Tuple<object, object, object>(obj["SameElement"], obj["SystemElement"], deviceID));
    }
    Best Regards,
    Li Wang

    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.

    • Marked as answer by Milsnips Friday, June 3, 2016 4:12 AM
    Friday, June 3, 2016 2:30 AM
    Moderator

All replies

  • Hi Milsnips,

    The WMI Query Language (WQL) is a subset of ANSI SQL — with some semantic changes. Not everything you can do in SQL is possible in WQL. To get the DeviceID, we could use following code.

    foreach (ManagementObject obj in searcher.Get())
    {
        var deviceID = new ManagementObject(obj["SystemElement"].ToString()).GetPropertyValue("DeviceID");
        list.Add(new Tuple<object, object, object>(obj["SameElement"], obj["SystemElement"], deviceID));
    }
    Best Regards,
    Li Wang

    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.

    • Marked as answer by Milsnips Friday, June 3, 2016 4:12 AM
    Friday, June 3, 2016 2:30 AM
    Moderator
  • Thanks that is exactly what i was looking for, just couldn't find how to get into the reference object.
    Friday, June 3, 2016 4:12 AM