2017-12-08 27 views
1

기본 경로의 트래픽을 전달하는 인터페이스에 대해 Windows 7 컴퓨터의 DNS 서버 목록을 업데이트하는 스크립트를 작성하고 있습니다.Win32_NetworkAdapter.Index 및 Win32_IP4RouteTable.InterfaceIndex가 일치하지 않습니다.

제가

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

로 라우트 테이블 엔트리를 선택 Win32_NetworkAdapterConfiguration 얻어 인터페이스 목록을 가정하면

$interfaces = Get-WmiObject Win32_NetworkAdapterConfiguration 

로 정의 $interfaces 변수에 난의 두 목록 가입을 예상 한 조건 $route.InterfaceIndex -eq $interface.Index. 그러나 색인이 일치하지 않는 것으로 나타났습니다.

경로 테이블은 다음과 같은 인터페이스를 정의하고있다 : 그러나 $interface 목록 그러나 Intel(R) PRO/1000 MT 한리스트 (11)이고, Intel(R) PRO/1000 MT #2 인덱스 13를 갖는 두 목록에

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 

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 
    ... 

을 갖는다 7 다른 목록에. 이 "7-11"불일치의 원인은 무엇일까요?

InterfaceIndex 속성의 description에서 색인이 일치해야합니다. 이 경로의 다음 홉의

InterfaceIndex

IP 주소를 입력합니다. 이 속성의 값은 InterfaceIndex 속성의 값인 과 같으며 경로의 다음 홉인 네트워크 인터페이스 을 나타내는 Win32_NetworkAdapter 및 Win32_NetworkAdapterConfiguration의 인스턴스와 같습니다.

+1

'$ interfaces | Format-Table InterfaceIndex, Index, ServiceName, IPAddress, Description'은'InterfaceIndex'와'Index'의 관계를 설명합니다. – JosefZ

+0

@JosefZ 고맙습니다. 나는 그것을 완전히 놓쳤다. –

답변

2

다음 코드는 -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에 그

  • 파이프 필터로 사용하기 위해 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'라는 속성이 없으므로 마지막 줄은 하나의 비어 있지 않은 열만 인쇄합니다. –

+0

@DmitriChubarov : 그것은 내 요점입니다. ;) –