2017-12-08 42 views
1

デフォルトルートのトラフィックを通過させるインターフェイスのWindows 7マシンでDNSサーバーリストを更新するスクリプトを作成しています。Win32_NetworkAdapter.IndexとWin32_IP4RouteTable.InterfaceIndexが一致しません

私は上の2つのリストを結合することが期待

$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リストは、インデックス13を有するIntel(R) PRO/1000 MT #2両方のリストにある

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一のリスト11であり、そして他のリストの7。この "7-11"の不一致の理由は何でしょうか?

InterfaceIndexプロパティのdescriptionから、インデックスが一致する必要があります。このルートのネクストホップの

ますInterfaceIndex

IPアドレス。このプロパティの値は、 のWin32_NetworkAdapterのインスタンスと、ルートの次のホップのネットワークインターフェイス を表す Win32_NetworkAdapterConfigurationのInterfaceIndexプロパティの値と同じです。

+1

'$ interfaces | Format-Table InterfaceIndex、Index、ServiceName、IPAddress、Description'は、 'InterfaceIndex'と' Index'の関係を説明する必要があります。 – JosefZ

+0

@JosefZありがとう、それは素晴らしいです。私はそれを完全に逃した。 –

答えて

2

次のコードスニペットは、-in comparison operatorを使用して2つのリストを結合するための可能なアプローチを示しています。

$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を指定するために、適切な-Filterパラメータに置き換えられますのでご注意ください。 WMI Query Language (WQL)の構文を使用し、
  • パイプをSelect-Objectに追加するのは、よく知られているプロパティの繰り返し出力を避けるためです。
+0

いいです。清楚な :) –

1

私はあなたがそれらの違いを理解することは、今これをチェックすべきだと思う:

$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 
+1

'Win32_IP4RouteTable'インスタンスは' Index'というプロパティを持たないので、最後の行は1つの空でない列だけを出力します。 –

+0

@DmitriChubarov:それが私の要点です。 ;) –

関連する問題