我正在编写一个脚本,该脚本将更新Windows 7计算机上的DNS服务器列表,用于为默认路由传递流量的接口。
我选择了路由表条目
$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上加入这两个列表。然而,我注意到指数不匹配。
路由表具有以下接口定义:
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个。“7-11”差异的原因是什么?
从描述 of InterfaceIndex属性来看,我希望索引应该匹配。
InterfaceIndex 这条路线下一跳的IP地址。该属性中的值与InterfaceIndex属性中表示路由下一跳的网络接口的Win32_NetworkAdapter和Win32_NetworkAdapterConfiguration实例中的值相同。
发布于 2017-12-08 11:09:14
下面的代码片段显示了使用比较算子连接这两个列表的可能方法。
$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参数,以指定用作筛选器的Where子句。使用查询语言(WQL)的语法,以及Select-Object的管道仅仅是为了避免重复输出众所周知的属性.发布于 2017-12-08 08:32:46
我认为你现在应该检查一下,以了解它们之间的区别:
$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_routeshttps://stackoverflow.com/questions/47708846
复制相似问题