Advanced Windows PowerShell Scripting Video Training

Wednesday, January 7, 2015

Today in my Windows 7 class here in the Kingdom of Bahrain, I was asked if it was possible to get the duplex mode of a NIC with PowerShell. OK, this normally would be easy.

1

2

Get-NetAdapter|

Select-Object-PropertyName,FullDuplex

NameFullDuplex

--------------

vEthernet (External)

Network BridgeTrue

Ethernet

Wi-FiTrue

Local Area Connection

Correction: The paragraph below is the original. This operation will not work on Windows 7. In testing, I believed that we had it right but now I believe that the final testing was against a Windows Server 2012 R2 machine. After fully upgrading a Windows 7 box to PowerShell 4, Root/StandardCimV2 is still not available. Please do not use this code against Windows 7. I am reaching out to the client where we developed this code to make sure we did in fact have success. At this time, they are not having any issues.

Easy right? OK, now lets throw this into the mix. The OS is Windows 7. This command comes from the NetAdapter module in Windows 8/2012. So I did a little research and found a CIM library that exposes the duplex mode of each adapter. In the ROOT\StandardCimv2 namespace there is a class called CIM_NetworkPort. That is cool except that Windows 7 comes with PowerShell 2 which cannot access CIM information. OK, no problem. All we need to do is to upgrade the PowerShell version on the Windows 7 client that will be executing this code. Now we can play.

1

Get-CimInstance-NamespaceROOT\StandardCimv2-ClassNameCIM_NetworkPort

NameInterfaceDescriptionifIndex Statu

s

------------------------------- -----

vEthernet (External)Hyper-V Virtual Ethernet Adapter #282 Up

Network BridgeMicrosoft Network Adapter Multiplexo...75 Up

EthernetIntel(R) 82579LM Gigabit Network Con...3 Di...

Wi-FiDell Wireless 1504 802.11b/g/n (2.4GHz)2 Up

Local Area ConnectionPdaNet Broadband Adapter12 Di...

Now we are cooking.

Doing a little bit of filtering and making the output readable, I adjusted the code a bit.

OK, I can’t leave it like this. I have two of my students drooling with PowerShell excitement and this is a Windows 7 class. Even though they should be paying attention, they are actively playing with PowerShell and asking questions about it during our labs. So, I thought that I would give them something to reverse engineer.