#PSTip Getting WMI associated classes in PowerShell 3.0

In Windows PowerShell 2.0, retrieving WMI associations and references required complex WMI queries. For example, if you wanted to get the IP address bound to a particular network adapter using WMI associations you would run:

$query = "ASSOCIATORS OF {Win32_NetworkAdapter.DeviceID=12} WHERE ResultClass=Win32_NetworkAdapterConfiguration"
Get-WmiObject -Query $query

This certainly requires a good understanding of WMI Query Language and how to use different keywords in WQL.

Enter Windows PowerShell 3.0!

PowerShell 3.0 includes CIM cmdlets and the above example can be easily implemented using the new Get-CimAssociatedInstance cmdlet.

Let us see how:

$net = Get-CimInstance -ClassName Win32_NetworkAdapter -Filter "DeviceID=12"
Get-CimAssociatedInstance -InputObject $net -ResultClassName Win32_NetworkAdapterConfiguration

That is it. Simple!

About the author: Ravikanth C

Ravikanth is the founder and editor of the PowerShell Magazine. He is also a PowerShell MVP who works at Dell Inc. He blogs at http://www.ravichaganti.com/blog and you can follow him on Twitter @ravikanth.

Related Posts

%d bloggers like this: