Condividi tramite


Getting Network Adapter Speed

TIMTOWTDI (pronounced ‘tim-toady’) is the Perl dictum, “There is more than one way to do it.”  PowerShell seems to be mercifully free of this, but this may be an exception.  We leverage the old standard “Win32_NetworkAdapterConfiguration” where “IPEnabled=’$true’”, but we also utilize [System.Net.NetworkInformationInterface]::GetAllNetworkInterfaces().  This .NET call is very useful – it reveals the speed, the MAC address, the number of bits sent and received.  However, it has to be run locally.  WinRM to the rescue!

 

function Get-NetworkAdapterSpeed { 
     [CmdletBinding()]
     param ( [parameter(ValueFromPipeline=$true,Position=0)][string[]]$ComputerName = @($env:COMPUTERNAME) ); 

     process {
         & { 
             foreach ($computer in $ComputerName) {
                 if (!$computer) { continue; }
                 $computer = $computer.ToLower();
                 Write-Progress (Get-Date) "Test-EthernetConfig -ComputerName $computer";

                 $scriptBlock = {
                     [Object[]]$wmiNACs = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "IPEnabled='$true'";
                     [Object[]]$dotNetNICs = [System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces() | ? { $_.OperationalStatus -eq 'Up' };

                     foreach ($wmiNAC in $wmiNACs) {
                         $dotNetNic = $dotNetNICs | ? { $_.Description -eq $wmiNAC.Description; }
                         New-Object -TypeName PSObject -Property @{
                             ComputerName = $env:COMPUTERNAME.ToLower();
                             Description = $dotNetNic.Description;
                             NetworkInterfaceType = $dotNetNic.NetworkInterfaceType;
                             Speed =  & {
                                 if (([string]$dotNetNic.Speed).Length -gt 9) {
                                     $mantissa = ([string]$dotNetNic.Speed).SubString(0, (([string]$dotNetNic.Speed).Length - 9));
                                     "$mantissa Gbps";
                                 } elseif (([string]$dotNetNic.Speed).Length -gt 6) {
                                     $mantissa = ([string]$dotNetNic.Speed).SubString(0, (([string]$dotNetNic.Speed).Length - 6));
                                     "$mantissa Mbps";
                                 } elseif (([string]$dotNetNic.Speed).Length -gt 3) {
                                     $mantissa = ([string]$dotNetNic.Speed).SubString(0, (([string]$dotNetNic.Speed).Length - 3));
                                     "$mantissa Kbps";
                                 } else {
                                     "$($dotNetNic.Speed) bps";
                                 }            
                             }
                         }
                         $wmiNA = $wmiNAC.GetRelated("Win32_NetworkAdapter")
                     }
                 } 
                 if ($computer -eq $env:COMPUTERNAME) {
                     & $scriptBlock;
                 } else {
                     Invoke-Command -HideComputerName -ComputerName $computer -ScriptBlock;
                 }
             }
         } | Select-Object -Property ComputerName, NetworkInterfaceType, Description, Speed;
     }
}