0

#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!

Filed in: Columns, Tips and Tricks Tags: , ,

Leave a Reply

Submit Comment

© 2016 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: