Friday, August 15, 2008

Getting Network Adapter Information Using Get-WMIObject and GetRelated()

WMI stores network adapter information in two classes: Win32_NetworkAdapter and Win32_NetworkAdapterConfiguration.  The former has basic information about the network adapter, and the latter has the IP configuration, etc.

The last time I had to do this, had to look up both objects and compare them to see that the property that linked the two classes was called Index and then write this to output the Win32_NetworkAdapter object, the associated Win32_NetworkAdapterConfiguration object, and then a line of dashes to separate the adapters.

gwmi Win32_NetworkAdapterConfiguration| %{gwmi -query "Select * from Win32_NetworkAdapter Where Index = $($_.Index)"; $_;  '------------------'}

As I discoverd from another post by /\/\o\/\/ (aka The PowerShell Guy), it turns out that the objects returned by Get-WmiObject have a nifty method called GetRelated that will do that work for you, so you can do this instead:

gwmi Win32_NetworkAdapterConfiguration | %{$_.GetRelated('Win32_NetworkAdapter'); $_;  '------------------'}

No comments: