Wednesday, March 14, 2012

Finding a physical network adapter with PowerShell in Windows 8

Get-NetAdapter

Okay, done.  That was a bit lackluster, wasn’t it?

First of all, try this:  Get-Command –Module NetAdapter
Okay, lots of cmdlets.  Recall all those intricate levels to netsh?  It is all here:  Get-Command –Noun Net*  but I only want to look at the physical NIC of the operating system.

Lets examine a Network Adapter object to determine what we can select on.

$nic = Get-NetworkAdapter (I am assuming that you only have one, if you have more than one $nic becomes an array and that changes inspection a bit)

If I type $nic I get (Name, InterfaceDescription, ifIndex, MacAddress, LinkSpeed):


Okay, but there are more properties.  Try this: $nic | Format-List


Okay, a bit more.  But there are actually more properties than that.  You will see this if you type $nic. and then hit TAB a bit.  Lots of properties.  Or:  Format-List –InputObject $nic –Property *

So, what can I select on here?  Name, Description, MAC, Up or Down or Connected, Link Speed.  Properties of the physical NIC adapter.  But I want to select a NIC based on the network it is attached to, where it this?

Lets go back to our list of Network cmdlets.  I see Get-NetIpAddress and Get-NetIpInterface.  Hmm..  I list those out and I see all kinds of stuff.  Loopback adapters, physical adapters, missing adapters.  Must be everything that is configured.

If I Get-NetIPInterface –Interface Wired* –ConnectionState Connected I filter down to those interfaces that are physically connected.

From here I see that I have an interface for the IPv6 address and an interface for the IPv4 address.  And they have the same ifIndex (physical NIC device).  But I am still stuck as the physical device and nothing with the DNS of the network or the IP or subnet that came from DHCP.  But, I have the ifIndex.

Lets capture that NetIPInterface object $nic = Get-NetIPInterface –Interface Wired* –ConnectionState Connected

Let’s focus on Get-NetIPAddress   If I Get-NetIPAddress I can select (or filter) using IPv4Address, Alias (Name), InterfaceAlias (the discovered DNS), and InterfaceIndex.  Yea!

Now, what are the IP addresses associated with this interface:  Get-NetIPAddress –InterfaceIndex $nic.ifIndex

I got both an IPv6 and IPv4 back and the Name, Preferred, and that it came from DHCP.  So, I could have selected here and went the other way to the interface as well.

Getting a bit more advanced and linking these two selection criteria together.

Get-NetIPAddress –IPv4Address 192* | Get-NetIPInterface

Or

Get-NetIPInterface | Get-NetIPAddress –IPv4Address 192*

The difference is the object you get back.  In the first one I get the Interface object as my result.  With the second one I get the IP Address object as the result.

I could also select my NICs using Get-NetAdapter.  For example the Intel NICs are always for VMs and I only want a physical NIC.

Get-NetAdapter -InterfaceDescription *Intel* –Physical

I can feed my other return into this to verify that what I got back also meets this criteria or compare the InterfaceIndex that was returned by both.  This way I know if the cable guys have a patch or port incorrect.

No comments: