Win32_NetworkAdapter.Index 和 Win32_IP4RouteTable.InterfaceIndex 不匹配

Win32_NetworkAdapter.Index and Win32_IP4RouteTable.InterfaceIndex do not match

我正在编写一个脚本,用于更新 Windows 7 台计算机上的 DNS 服务器列表,用于为默认路由传递流量的接口。

我选择了路线 table 条目

$defaultgw_routes = Get-WMIObject Win32_IP4RouteTable | 
    Where-Object {$_.Destination -eq "0.0.0.0"}

并假设使用 Win32_NetworkAdapterConfiguration 获得的接口列表在定义为

$interfaces 变量中
$interfaces = Get-WmiObject Win32_NetworkAdapterConfiguration

我希望加入两个列表,条件是 $route.InterfaceIndex -eq $interface.Index。但是我注意到索引不匹配。

路由table具有以下接口定义:

C:\Users\user01>route print if 11
===========================================================================
Interface list
  ....
 13...08 00 27 8d 7e 19 ......Intel(R) PRO/1000 MT #2
 11...08 00 27 a4 16 ad ......Intel(R) PRO/1000 MT
 12...00 00 00 00 00 00 00 e0 Teredo Tunneling Pseudo-Interface
  ...

但是 $interface 列表有

ServiceName      : E1G60
Description      : Intel(R) PRO/1000 MT
Index            : 7

ServiceName      : tunnel
Description      : Tunnel adapter Microsoft Teredo
Index            : 11

ServiceName      : E1G60
Description      : Intel(R) PRO/1000 MT #2
Index            : 13

即在两个列表中 Intel(R) PRO/1000 MT #2 的索引为 13,但是 Intel(R) PRO/1000 MT 在一个列表中为 11,在另一个列表中为 7。 "seven-eleven" 差异的原因可能是什么?

InterfaceIndex 属性 的 description 我希望索引应该匹配。

InterfaceIndex

IP address of the next hop of this route. The value in this property is the same as the value in the InterfaceIndex property in the instances of Win32_NetworkAdapter and Win32_NetworkAdapterConfiguration that represent the network interface of the next hop of the route.

我认为您现在应该检查一下以了解它们之间的区别:

$interfaces = Get-WmiObject Win32_NetworkAdapterConfiguration | select InterfaceIndex, Index
$defaultgw_routes = Get-WMIObject Win32_IP4RouteTable |?{$_.Destination -eq "0.0.0.0"} | Select InterfaceIndex,Index

$interfaces 
$defaultgw_routes

以下代码片段显示了使用 -in comparison operator 连接两个列表的可能方法。

$filterDest = "Destination = '0.0.0.0'" 
$defaultgw_routes = Get-WMIObject Win32_IP4RouteTable -Filter $filterDest
$filterIPA  = "IPEnabled = True"
$interfaces = Get-WmiObject Win32_NetworkAdapterConfiguration -Filter $filterIPA

'### interfaces ###'
$interfaces | 
    Where-Object {$_.InterfaceIndex -in $defaultgw_routes.Interfaceindex } |
       Select-Object [a-z]* -ExcludeProperty PSComputerName, Scope, Path, Options, 
           ClassPath, Properties, SystemProperties, Qualifiers, Site, Container
'### routes ###'
$defaultgw_routes | 
    Where-Object {$_.InterfaceIndex -in $interfaces.Interfaceindex } | 
       Select-Object [a-z]* -ExcludeProperty PSComputerName, Scope, Path, Options, 
           ClassPath, Properties, SystemProperties, Qualifiers, Site, Container

请注意

  • Where-Object 的管道被替换为适当的 -Filter 参数以指定 Where 子句 用作过滤器。使用 WMI Query Language (WQL)
  • 的语法
  • 添加到 Select-Object 的管道只是为了避免重复输出众所周知的属性。