{"id":12241,"date":"2011-10-30T00:01:00","date_gmt":"2011-10-30T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/10\/30\/use-wmi-association-classes-to-relate-two-classes-together\/"},"modified":"2011-10-30T00:01:00","modified_gmt":"2011-10-30T00:01:00","slug":"use-wmi-association-classes-to-relate-two-classes-together","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-wmi-association-classes-to-relate-two-classes-together\/","title":{"rendered":"Use WMI Association Classes to Relate Two Classes Together"},"content":{"rendered":"<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\"><strong>Summary:<\/strong> Use a cool Windows PowerShell technique to easily relate two WMI classes together.<\/span><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">Microsoft Scripting Guy Ed Wilson here. Well, the Scripting Wife and I are getting settled back down from our Canadian tour. We had a great time visiting MVPs on our return. We got to see Jeffery Hicks in Syracuse, Joel Bennett in Rochester, and Ken McFerron in Pittsburgh, Pennsylvania. While teaching my Windows PowerShell class in Montreal, I realized that I had not written very much about using Windows PowerShell with WMI association classes. <\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">When querying a WMI association class, the results return references to external classes. In fact, the things that are returned point to the key property of the referenced classes. An example will help to clarify this. My laptop has a network adapter. The <b>win32_networkadapter<\/b> WMI class describes the maker, the speed, MAC address, and other information about my network adapter. The following query returns information about my network adapter:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">gwmi win32_networkadapter -Filter &#8220;netconnectionid = &#8216;local area connection'&#8221;<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">If I want to find all of the information about my network adapter, but I do not want to see any empty properties (because some WMI classes contain literally hundreds of properties many of which do not return any information), I use the <b>haswmivalue<\/b><i> <\/i>filter from my WMI module. <\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">In the following figure, I use the <b>haswmivalue<\/b> filter from <\/span><span style=\"font-family: verdana,geneva\"><a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/Scripting-Guys-WMI-helper-bf3cc6d6\"><span>my HSGWMImoduleV5 module<\/span><\/a><\/span><span style=\"font-family: Segoe\"> to return all the properties that contain a value. (Of course, the <b>haswmivalue<\/b> function also exists in <\/span><span style=\"font-family: verdana,geneva\"><a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/WMI-Helper-Module-for-90e4f22e\"><span>HSGWMImoduleV6<\/span><\/a><\/span><span style=\"font-family: Segoe\"> as well. Version six of my HSG WMI Module is the latest version, and it includes several upgrades to version five). <\/span><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3487.hsg-10-30-11-1.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of using haswmivalue filter\" alt=\"Image of using haswmivalue filter\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3487.hsg-10-30-11-1.png\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">In addition to having physical information, such as the MAC address and speed, a network adapter also has a protocol (or more than one protocol) bound to it. To find protocol type of information, such as the TCP\/IP address, subnet mask, and default gateway, it is necessary to query the <b>Win32_NetworkAdapterConfiguration<\/b> WMI class. To make matters worse, the <b>netconnectionid<\/b><i> <\/i>property that I used when querying the <b>Win32_NetworkAdapter<\/b> WMI class is not available. In addition, the <b>key<\/b> property from <b>Win32_NetworkAdapter<\/b> is <b>DeviceID<\/b>, but the <b><span>key<\/span><\/b> property from <b>Win32_NetworkAdapterConfiguration<\/b> is <b>index<\/b><i>. <\/i>Upon closer observation, however, this problem is not as bad as it might seem. Using my <b>Get-WmiKeyValue<\/b> function from my <b>HSGWmiModuleV6<\/b> WMI module, I can easily see that the properties from the two different WMI classes are probably related (the values seem to be identical). This is shown in the following figure.<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0576.hsg-10-30-11-02.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image showing that properties from the two WMI classes are probably related\" alt=\"Image showing that properties from the two WMI classes are probably related\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0576.hsg-10-30-11-02.png\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">From my previous query of <b>Win32_NetworkAdapter<\/b>, I saw that the <b>deviceID<\/b><i> <\/i>of 7 was the network interface from which I wanted to obtain information. Using this value as the <b>index<\/b> number, I come up with the following query (<b>gwmi<\/b> is an alias for <b>Get-WmiObject<\/b>). The first position is <b>class<\/b><i> <\/i>and I left it out. The <b>f<\/b><i> <\/i>stands for <b>filter<\/b><i> <\/i>and I was able to use the abbreviation. The <b>haswmivalue<\/b><i> <\/i>filter is from my WMI module.<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">gwmi win32_networkadapterconfiguration -F &#8220;index = 7&#8221; | HasWmiValue<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">The command and associated output are shown in the following figure.<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1588.hsg-10-30-11-03.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of command and associated output\" alt=\"Image of command and associated output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1588.hsg-10-30-11-03.png\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">What is needed is a way to query both WMI classes at the same time. I need to see both the hardware information and the protocol related information for the same adapter. There is a solution for this problem; I can use an association WMI class. In the old VBScript days, querying a WMI association class was a really hard-core task that few outside of the Microsoft Scripting Guys really understood. This can all change in the Windows PowerShell world, because querying WMI association classes is trivial. Keep in mind that there are more than 100 association classes in <b>root\\cimv2<\/b> alone. These classes expose very useful and powerful information about your system. <\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">Using my <b>Get-WmiClassesWithQualifiers<\/b> function from my WMI module, I can easily find association classes in any WMI namespace. For our purposes, I will focus on association classes in <b>Root\\CimV2<\/b> (the default WMI namespace.) I am only interested in working with dynamic<i> <\/i>WMI classes, and unfortunately, I did not add a dynamic filter to my <b>Get-WmiClassesWithQualifiers<\/b> function (this is something I will probably do at a later date). In the meantime, I can reduce the number of WMI classes I need to filter through by removing the classes that begin with <i>cim. <\/i>In most cases, classes that begin with <i>cim <\/i>are abstract classes. I use the following query to find association<i> <\/i>classes in <b>Root\\CimV2<\/b>. <\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">Get-WMIClassesWithQualifiers -qualifier association | ? { $_ -notmatch &#8216;^cim&#8217;} | sort <\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">The command and associated output are shown in the following figure.<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7711.hsg-10-30-11-04.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of command and associated output\" alt=\"Image of command and associated output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7711.hsg-10-30-11-04.png\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">Exploration of these association classes will yield fruitful and profitable results. To illustrate working with these classes, I want to examine the <b>Win32_NetworkAdapterSetting<\/b> association class. This class relates the two WMI classes we began with: <b>Win32_NetworkAdapter<\/b> and <b>Win32_NetworkAdapterConfiguration<\/b>. <\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">The first thing I do is query the <b>Win32_NetworkAdapterSetting<\/b> WMI class directly by using the <b>Get-WMIObject<\/b> cmdlet. This query is shown here (using the <b>gwmi<\/b> alias for <b>Get-WmiObject<\/b>):<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">gwmi win32_networkadaptersetting<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">The command and associated output are shown in the following figure.<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2664.hsg-10-30-11-05.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of command and associated output\" alt=\"Image of command and associated output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2664.hsg-10-30-11-05.png\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">A close examination of both the <b>element<\/b><i> <\/i>and the <b>setting<\/b><i> <\/i>properties reveals they point to the <b>key<\/b> properties of the <b>Win32_NetworkAdapter<\/b> and <b>Win32_NetworkAdapterConfiguration<\/b> WMI classes. It is possible to filter out using WQL, but it starts to get tricky, and it is therefore easier to use the <b>Where-Object<\/b> cmdlet to find an element that has the <b>deviceID<\/b> of 7 (the value we found earlier). This is really easy to do, and the command is shown here (I use <b>gwmi<\/b> for <b>Get-WmiObject<\/b> and <b>?<\/b> for <b>Where-Alias<\/b>):<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">gwmi win32_networkadaptersetting | ? {$_.element -match 7}<b><\/b><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">The <b>[WMI]<\/b> management accelerator accepts this fully qualified path to a management object, and will return the associated class. To query these, I first store the result from the previous command into a variable called <b>$adapter<\/b>. Next, I use the <b>[WMI]<\/b> type accelerator to return the specific WMI instance of my <b>Win32_NetworkAdapter<\/b> and my <b>Win32_NetworkAdapterConfiguration<\/b> classes. Here are the three commands:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">$adapter = gwmi win32_networkadaptersetting | ? {$_.element -match 7}<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">[wmi]$adapter.Element | HasWmiValue<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">[wmi]$adapter.Setting | HasWmiValue<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">The following figure illustrates these three commands and the associated output. <\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5618.hsg-10-30-11-06.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of three commands and associated output\" alt=\"Image of three commands and associated output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5618.hsg-10-30-11-06.png\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">One other thing before I go. It is possible to perform a less direct filter. This is because using the <b>[WMI]<\/b> type accelerator and the path, I obtain a complete instance of the WMI class that is referenced. This means I can use the same type of query that I used previously with <b>Get-WMIObject<\/b> when querying the WMI classes directly. Perhaps an example will suffice. In the following code, I use the <b>Get-WMIObject<\/b> (<b>gwmi<\/b> is the alias) to query the Win32_NetworkAdapterSetting WMI class. I use the where-object cmdlet (? is the alias) to perform my filtering. I already know that the first command, <b>gwmi win32_networkadaptersetting<\/b>, returns a collection of paths that point to the key values of all instances of <b>Win32_NetworkAdapter<\/b> and <b>Win32_NetworkAdapterConfiguration<\/b>. I choose the value stored in the <b>element<\/b><i> <\/i>property because it points to instances of <b>Win32_NetworkAdapter<\/b>. I can therefore use one of my favorite techniques that I call &ldquo;group and dot&rdquo; where I group the results by using parentheses, and I access a specific property by using dotted notation. After I have the specific property, I look for elements that have a value of the <b>netconnectionid<\/b><i> <\/i>property that will match <i>local area connection. <\/i>The command is shown here:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">gwmi win32_networkadaptersetting | ? {([wmi]$_.element).netconnectionID -match &#8216;local area connection&#8217;}<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">The command and associated output are shown in the following figure.<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6644.hsg-10-30-11-07.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of command and associated output\" alt=\"Image of command and associated output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6644.hsg-10-30-11-07.png\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">Well, that is all there is to working with WMI association classes. Hopefully, you will search for other association classes using the techniques I have outlined here, and then you will also begin to explore the type of information that is available. <\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">I invite you to follow me on <\/span><span style=\"font-family: verdana,geneva\"><a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\"><span>Twitter<\/span><\/a><\/span><span style=\"font-family: Segoe\"> and <\/span><span style=\"font-family: verdana,geneva\"><a href=\"http:\/\/bit.ly\/scriptingguysfacebook\"><span>Facebook<\/span><\/a><\/span><span style=\"font-family: Segoe\">. If you have any questions, send email to me at <\/span><span style=\"font-family: verdana,geneva\"><a href=\"mailto:scripter@microsoft.com\" target=\"_blank\"><span>scripter@microsoft.com<\/span><\/a><\/span><span style=\"font-family: Segoe\">, or post your questions on the <\/span><span style=\"font-family: verdana,geneva\"><a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\"><span>Official Scripting Guys Forum<\/span><\/a><\/span><span style=\"font-family: Segoe\">. See you tomorrow. Until then, peace.<\/span><\/span><\/p>\n<\/p>\n<p><span style=\"font-size: small\"><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><\/span>&nbsp;<\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Use a cool Windows PowerShell technique to easily relate two WMI classes together. &nbsp; Microsoft Scripting Guy Ed Wilson here. Well, the Scripting Wife and I are getting settled back down from our Canadian tour. We had a great time visiting MVPs on our return. We got to see Jeffery Hicks in Syracuse, Joel [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,45],"class_list":["post-12241","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Use a cool Windows PowerShell technique to easily relate two WMI classes together. &nbsp; Microsoft Scripting Guy Ed Wilson here. Well, the Scripting Wife and I are getting settled back down from our Canadian tour. We had a great time visiting MVPs on our return. We got to see Jeffery Hicks in Syracuse, Joel [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12241","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=12241"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12241\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=12241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=12241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=12241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}