Monday, May 21, 2012

Finding and sorting Server 2012 NICs with PowerShell

If you are using Server 2008 (any release) you should just go on now, this is about PowerShell v3 in Server 2012.

You can stay to see how easy this is becoming though.

So, I came up with this one liner – I am pretty proud of it, I am not a developer and not a big fan of one-liners as they are difficult to pass to other folks for them to understand, but this works.

$ips = Get-NetAdapter -Physical | where {$_.Status -eq "Up"} | Get-NetIPConfiguration | Group-Object -property IPv4DefaultGateway

Now, what does this give me?

This gives me an array (a couple levels deep due to the Group (which I had never used before).

I end up with an array of Groups.  Each Group is named with the IPv4 Default Gateway of the NICs.

Within the group is a list of NICs that all have that IPv4 default gateway in common.

The one liner first Gets the Network Adapter object for only the Physical NICs that are jacked ( ‘up’ ).

Get-NetAdapter -Physical | where {$_.Status -eq "Up"}

The NICs that meet that criteria – I then get the NetIPConfiguration of (as this is where the Gateway property is hiding).  And I group them based on the IPvDefaultGateway. 

Get-NetIPConfiguration | Group-Object -property IPv4DefaultGateway

My assumption here is that if the NIC is ‘up’ it has a DHCP address.

This is all fine and good you think, but how is this real?  What is the scenario? 

Someone racked this server, they jacked it to the management network, the VM production network, and the storage network.  I want to know what NICs are jacked where and group and summarize them.  And each of these networks has DHCP running as I have no time to manually assign IP addresses any longer.  They are divided physically in the top of the rack with three switches.  (two posts into the future will use this).

No comments: