【问题标题】:PowerShell script to get network card speed of a Windows ComputerPowerShell脚本获取Windows计算机的网卡速度
【发布时间】:2010-06-09 01:05:26
【问题描述】:

获取特定 Windows 机器网卡运行速度的 PowerShell 脚本是什么?

我知道这可以通过基于 WMI 查询的语句来完成,一旦我解决了就会发布答案。

【问题讨论】:

    标签: windows powershell wmi


    【解决方案1】:

    一个基本的命令是

    Get-WmiObject -ComputerName 'servername' -Class Win32_NetworkAdapter | `
        Where-Object { $_.Speed -ne $null -and $_.MACAddress -ne $null } | `
        Format-Table -Property SystemName,Name,NetConnectionID,Speed
    

    请注意,ComputerName 参数采用一个数组,因此您可以在拥有权限的多台计算机上运行它。将 Format-Table 属性列表替换为 ***** 以获得更全面的可用属性列表。您可能希望过滤这些属性以删除您不感兴趣的条目。

    根据您的需要,使用内置的字节乘数后缀(MB、GB 等)也会使速度更具可读性。您可以将其指定为 Format-Table -Property 数组上的 HashTable 条目,例如

    Format-Table -Property NetConnectionID,@{Label='Speed(GB)'; Expression = {$_.Speed/1GB}}
    

    【讨论】:

    • 一个不错的答案!我会删除 -ne $null 部分。
    • Win32_NetworkAdapter 类实际上返回了相当多的条目,检查一下。我正在使用 -ne $null 过滤掉条目,以便我可以看到真实实体卡的实际条目,但您可以根据需要进行调整。
    • [System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces() 仅适用于本地计算机。
    • 我认为 Jay 是说过滤器可以简化为 Where {$_.Speed -and $_.MACAddress}。只要既不是 0 也不是 $false,简化的谓词就可以正常工作。此外,您可能还是希望过滤掉 0 的值。
    • 它显然也将允许值 0。这应该是唯一的区别。
    【解决方案2】:

    从 Windows 8/Server 2012 开始,您可以尝试Get-NetAdapter 和一些更专业的命令,例如Get-NetAdapterAdvancedProperty

    https://docs.microsoft.com/en-us/powershell/module/netadapter/get-netadapter

    您还可以使用更全面的 WMI 类 MSFT_NetAdapter 来创建自定义输出。 MSFT_NetAdapter 在这里描述:

    https://msdn.microsoft.com/en-us/library/Hh968170(v=VS.85).aspx

    这是一个列出本地计算机上已启用 (State 2)、已连接 (OperationalStatusDownMediaDisconnected $false)、802.3 有线 (NdisPhysicalMedium 14)、非虚拟适配器的速度和其他属性的命令:

    Get-WmiObject -Namespace Root\StandardCimv2 -Class MSFT_NetAdapter | `
      Where-Object { $_.State -eq 2 -and $_.OperationalStatusDownMediaDisconnected -eq $false -and `
                     $_.NdisPhysicalMedium -eq 14 -and $_.Virtual -eq $false } | `
      Format-Table Name,Virtual,State,NdisPhysicalMedium, `
      @{Label='Connected'; Expression={-not $_.OperationalStatusDownMediaDisconnected}}, `
      @{Label='Speed(MB)'; Expression = {$_.Speed/1000000}}, `
      FullDuplex,InterfaceDescription
    

    【讨论】:

      【解决方案3】:

      我目前的版本,取出蓝牙和无线网卡(使用powershell -file script.ps1运行):

      # return network speed as exit code
      
      $speed = Get-WmiObject -Class Win32_NetworkAdapter | 
      where { $_.speed -and $_.macaddress -and 
      $_.name -notmatch 'wireless|wi-fi|bluetooth|802\.11' } | select -expand speed
      exit $speed/1000000
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-22
        • 2015-03-28
        • 2017-11-30
        • 2017-01-01
        • 2021-12-09
        • 1970-01-01
        • 1970-01-01
        • 2020-06-05
        相关资源
        最近更新 更多