WMI CookBook: Associators Pt I

WMI is a wonderful thing and like many people I have a love-hate relationship with it. It is incredibly powerful, and can take you deep into the system, but it is not easy to find your way.

I have decided to spend some time digging into WMI and will document the findings in a (long) series of blog posts. I also intend to pull the information together into a document that will be available for download.  The scripts will end up in PowerShell modules that will also be available for download.

I am doing this with PowerShell v2 on Windows 7. Where necessary, and wherever possible, I will give alternatives that can be used with PowerShell v1.

The main trick with WMI is finding out the class or classes we need to actually use. The standard PowerShell way of searching for classes is to use the –List parameter.  Network Adapters are common items we need to deal with so we’ll see what WMI can tell us.  First, what WMI classes deal with Network Adapters. One thing we can do with –List is give a partial class name and a wildcard to restrict the results (otherwise we get lots, and lots of classes returned).  To begin with I’m only interested in the name of the class. I’m working in the default name space so don’t need to mention it.

PS> Get-WmiObject -List "Win32_NetworkAdapter*" | Select Name

Name
----
Win32_NetworkAdapter
Win32_NetworkAdapterConfiguration
Win32_NetworkAdapterSetting

We could now go off to MSDN and check what these classes do but we can find that information directly.  As Jeffrey showed recently http://blogs.msdn.com/powershell/archive/2009/08/30/exploring-wmi-with-powershell-v2.aspx we can use the –Amended parameter to show the description property.

001
002
003
Get-WmiObject -List "Win32_NetworkAdapter*" | foreach {
    (( Get-WmiObject -List $_.Name  -Amended ).Qualifiers | Where {$_.Name -eq "Description"}).Value
}

 

This gives use this information for the three classes.

The Win32_NetworkAdapter class represents a network adapter on a Win32 system.
The Win32_NetworkAdapterConfiguration class represents the attributes and behaviors of a network adapter. This class has been extended to include extra properties and methods that support the management of the TCP/IPprotocols (and are independent of the network adapter).
The Win32_NetworkAdapterSetting class represents an association between a network adapter and its configuration settings.

If we look at Win32_NetworkAdapterSetting

Get-WmiObject Win32_NetworkAdapterSetting

we will see an entry for each adapter that links the adapter with its configuration. This information is paired like this:

Element          : \\RSLAPTOP01\root\cimv2:Win32_NetworkAdapter.DeviceID="11"
Setting          : \\RSLAPTOP01\root\cimv2:Win32_NetworkAdapterConfiguration.Index=11

If we look at the adapter and its configuration

Get-WmiObject -Class Win32_NetworkAdapter -Filter "DeviceID='11'"

Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "Index='11'"

we can see the individual information.  Going through the Win32_NetworkAdapterSetting class is tedious but now we now that the adapter’s DeviceId is linked to the Configurations Index we can use this ASSOCIATION via a bit of WQL.

Get-WmiObject -Query "ASSOCIATORS OF {Win32_NetworkAdapter.DeviceID='11'} WHERE ResultClass=Win32_NetworkAdapterConfiguration"

DHCPEnabled      : True
IPAddress        : {192.168.196.138, fe80::6d95:b824:6a72:a0a9}
DefaultIPGateway : {192.168.196.1}
DNSDomain        :
ServiceName      : athr
Description      : Atheros AR5007 802.11b/g WiFi Adapter
Index            : 11

The other thing to think about is what other associations exist.

PS> Get-WmiObject -Query "ASSOCIATORS OF {Win32_NetworkAdapter.DeviceID='11'} " | Select __CLASS -Unique

__CLASS
-------
Win32_PnPEntity
Win32_ComputerSystem
Win32_NetworkAdapterConfiguration
Win32_IRQResource
Win32_DeviceMemoryAddress
Win32_NetworkProtocol
Win32_SystemDriver

So we have a set of classes that are all associated through the network adpater device id.   In the next few posts we will dig into this web and see what we can discover and how we can use these associations to help us understand our system.

Technorati Tags: ,,
Published Mon, Nov 16 2009 21:03 by RichardSiddaway

Leave a Comment

(required) 
(required) 
(optional)
(required)