Reputation: 17169
I am writing a script that would update DNS servers list on Windows 7 machines for the interfaces that pass traffic for default routes.
I have selected the route table entries with
$defaultgw_routes = Get-WMIObject Win32_IP4RouteTable |
Where-Object {$_.Destination -eq "0.0.0.0"}
and assuming the list of interfaces obtained with Win32_NetworkAdapterConfiguration
is in the $interfaces
variable defined as
$interfaces = Get-WmiObject Win32_NetworkAdapterConfiguration
I expected to join the two lists on condition $route.InterfaceIndex -eq $interface.Index
. However I noticed that the indices do not match.
The route table has the following interface definitions:
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
...
However the $interface
list has
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
That is in both lists Intel(R) PRO/1000 MT #2
has index 13, however Intel(R) PRO/1000 MT
is 11 in one list and 7 in the other list. What could be the reason for this "seven-eleven" discrepancy?
From the description of the InterfaceIndex
property I would expect that the indices should match.
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.
Upvotes: 1
Views: 876
Reputation: 30113
The following code snippet shows a possible approach to to join the two lists using -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
Please note that
Where-Object
is replaced with appropriate -Filter
parameter to specify a Where
clause to use as a filter. Uses the syntax of the WMI
Query Language (WQL), and Select-Object
is added merely to avoid recurring output of well-known properties.Upvotes: 2
Reputation: 9133
I think you should check this now to understand the difference between them:
$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
Upvotes: 1