Advanced Windows PowerShell Scripting Video Training

Advanced Windows PowerShell Scripting Video Training
Advanced Windows PowerShell Scripting Video Training

Wednesday, January 7, 2015

Getting The NIC Duplex Mode with PowerShell

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 -Property Name, FullDuplex
Name                                                                   FullDuplex                                                           
----                                                                   ----------                                                           
vEthernet (External)                                                                                                                        
Network Bridge                                                         True                                                                 
Ethernet                                                                                                                                    
Wi-Fi                                                                  True                                                                 
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 -Namespace ROOT\StandardCimv2 -ClassName CIM_NetworkPort
Name                      InterfaceDescription                    ifIndex Statu
                                                                          s   
----                      --------------------                    ------- -----
vEthernet (External)      Hyper-V Virtual Ethernet Adapter #2          82 Up  
Network Bridge            Microsoft Network Adapter Multiplexo...      75 Up  
Ethernet                  Intel(R) 82579LM Gigabit Network Con...       3 Di...
Wi-Fi                     Dell Wireless 1504 802.11b/g/n (2.4GHz)       2 Up  
Local Area Connection     PdaNet Broadband Adapter                     12 Di...

Now we are cooking.
Doing a little bit of filtering and making the output readable, I adjusted the code a bit.
1
2
3
4
5
6
7
8
9
Get-CimInstance -Namespace ROOT\StandardCimv2 -ClassName CIM_NetworkPort -ComputerName "." |
ForEach-Object {
    $Obj = New-Object -TypeName PSObject -Property @{
        "ComputerName" = "."
        "Adapter" = $_.Name
        "FullDuplex" = $_.FullDuplex
    }
    Write-Output $Obj
    }
FullDuplex                   ComputerName                 Adapter                   
----------                   ------------                 -------                   
                             .                            vEthernet (External)      
True                         .                            Network Bridge            
                             .                            Ethernet                   
True                         .                            Wi-Fi                     
                             .                            Local Area Connection   

Mission accomplished!
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104

Function Get-AdapterDuplex
{
[CmdletBinding()]
Param(
    [parameter(Mandatory=$true,
                ValueFromPipeline=$true)]
    [String[]]
    $ComputerName

)
BEGIN
{
    # PowerShell 3 is required.
    If ($PSVersionTable.PSVersion.Major -lt 3)
    {
        $String1 = "You need at least Windows 3.0 to execute this command `n"
        $String1 += "You are running PowerShell $($PSVersionTable.PSVersion.Major)."
        Write-Host $String1  -BackgroundColor DarkRed
        BREAK

    }

    # Creates a new object to send to the pipeline.
    Function New-AdapterDuplexObject
    {
        $Obj = New-Object -TypeName PSObject -Property @{
        "ComputerName" = $Null
        "Adapter" = $Null
        "FullDuplex" = $Null
        "Online" = $False
        }
        $obj.PSObject.TypeNames.Insert(0,'AdapterDuplexObject')
        Write-Output $Obj

    }
} # END: BEGIN

PROCESS
{

    # Loop though each computer and test to see if it is online.
    ForEach ($C in $ComputerName)
    {
        # Create a Hash for Get-CIMInstance.
        $Hash = @{
            "Namespace" = "ROOT\StandardCimv2"
            "ClassName" = "CIM_NetworkPort"
            "ComputerName" = $C
            "ErrorAction" = "STOP"
        }
   
        Try
        {
            # If the client is online.
            Get-CimInstance @Hash |
            ForEach-Object {
                $Obj = New-AdapterDuplexObject
                $Obj.ComputerName = $C
                $Obj.Adapter = $_.Name
                $Obj.FullDuplex = $_.FullDuplex
                $Obj.Online = $True
       
            Write-Output $Obj
            }
        } # END: Try
        Catch
        {
            # If the client is offline.
            $Obj = New-AdapterDuplexObject
            $Obj.ComputerName = $C
            $Obj.Online = $False
           
            Write-Output $Obj
        } # END: Catch
    } # END: ForEach ($C in $ComputerName)
} # END: PROCESS

<#
.SYNOPSIS
Determines the network adapters current duplex state.

.DESCRIPTION
Determines the network adapters current duplex state.

.PARAMETER Computername
The name of the clients that you want to scan for the duplex state
of each network adapter.

.EXAMPLE
"LON-DC2","Test","LON-DC1" | Get-AdapterDuplex
FullDuplex   ComputerName   Online Adapter                       
----------   ------------   ------ -------                       
      TRUE   LON-DC2          True Ethernet                      
             Test            False                               
     FALSE   LON-DC1          True Ethernet 3                    
     FALSE   LON-DC1          True Ethernet                      
      TRUE   LON-DC1          True Ethernet 2  

Displays the current duplex state of three clients.  If the client is a virtual machine, the value will be NULL.

.NOTES
PowerShell 3.0 is required on the client running this command.
#>
} # END: Function Get-AdapterDuplex


Have fun with this guys!