Skip to main content

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!

Comments

Popular posts from this blog

Adding a Comment to a GPO with PowerShell

As I'm writing this article, I'm also writing a customization for a PowerShell course I'm teaching next week in Phoenix.  This customization deals with Group Policy and PowerShell.  For those of you who attend my classes may already know this, but I sit their and try to ask the questions to myself that others may ask as I present the material.  I finished up my customization a few hours ago and then I realized that I did not add in how to put a comment on a GPO.  This is a feature that many Group Policy Administrators may not be aware of. This past summer I attended a presentation at TechEd on Group Policy.  One organization in the crowd had over 5,000 Group Policies.  In an environment like that, the comment section can be priceless.  I always like to write in the comment section why I created the policy so I know its purpose next week after I've completed 50 other tasks and can't remember what I did 5 minutes ago. In the Group Policy module for PowerShell V3, th

Return duplicate values from a collection with PowerShell

If you have a collection of objects and you want to remove any duplicate items, it is fairly simple. # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   # Remove the duplicate values. $Set1 | Select-Object -Unique 1 2 3 4 5 6 7 What if you want only the duplicate values and nothing else? # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   #Create a second collection with duplicate values removed. $Set2 = $Set1 | Select-Object -Unique   # Return only the duplicate values. ( Compare-Object -ReferenceObject $Set2 -DifferenceObject $Set1 ) . InputObject | Select-Object – Unique 1 2 This works with objects as well as numbers.  The first command creates a collection with 2 duplicates of both 1 and 2.   The second command creates another collection with the duplicates filtered out.  The Compare-Object cmdlet will first find items that are diffe

How to list all the AD LDS instances on a server

AD LDS allows you to provide directory services to applications that are free of the confines of Active Directory.  To list all the AD LDS instances on a server, follow this procedure: Log into the server in question Open a command prompt. Type dsdbutil and press Enter Type List Instances and press Enter . You will receive a list of the instance name, both the LDAP and SSL port numbers, the location of the database, and its status.