PowerShell: Identifying Hard-Wired Network Connections

As is often with scripting, automation and tool making, we find ourselves needing to ensure we have a stable (wired) network connection before performing certain tasks on a system.  Whether it be for automating a domain join or simply copying a large file, ensuring you have that hard-wired connection can be critical.

After researching the internet and interrogating WMI on a few different test systems, I concluded that leveraging the Win32_NetworkAdapterConfigurations class was going to be my best option.  In one line of code we can get a collection of all hard-wired network connections on a system.

$NetworkConnections=Get-WmiObject-Class Win32_NetworkAdapterConfiguration -Filter“IPEnabled=’TRUE'”|Where-Object {($_.Description -notlike“*VMware*”) -and ($_.Description -notlike“*Wireless*”)}

Now to explain some of the filter choices I used.  The first is obvious.  IPEnabled=True limits the list of interfaces dramatically by only showing those with an IP Address.

Next we jump into the Where-Object cmdlet.  (Yes, I know in PowerShell 3 I could have used the shortened version of this but I also needed to support systems still on PowerShell v2).  In the Where-Object cmdlet we leverage the Description property and strip out any Wireless and VMware network adapters since these may show up in the list, but we know they aren’t wired.

Now, you may be asking yourself “Why not just filter out the word ‘Virtual’ to cover all platforms?”.  Well, because of Hyper-V, that’s why.  You see Hyper-V actually takes over your network connection when you bind it so all network traffic on your system (physical and virtual) are routed thru the Virtual Switch.  So if you have Hyper-V enabled, you could have a Network Connection named something like this:

Win32_NetworkAdapterConfiguration

As you can see, my Hyper-V Virtual Ethernet Adapter has the word “Virtual” in it, so we don’t want to exclude that from our search results.

Advertisements

Author: dhedges

I'm a Senior Client Systems Engineer specializing in OS Deployments and Automation using VBScript, PowerShell, MDT and SCCM. I enjoy working with technology and bending it to my will.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s