Powershell:如何找出公共接口的适配器索引?

Jon*_*ica 3 powershell

我使用 powershell 脚本手动设置我的 DNS

这很好用,但是当我在 wifi 和两个不同的扩展坞之间移动时,get-NetAdapter 中的“ifIndex”发生了变化。

仅仅过滤“Up”不会飞,因为有多个Up接口(见下文)

我想我需要检测既是 Up 又是公共路由的接口(例如,在通往 google.com 或公共的路由上)。

这该怎么做?

PS C:\Users\joe> get-netadapter

Name                      InterfaceDescription                    ifIndex Status       MacAddress             LinkSpeed
----                      --------------------                    ------- ------       ----------             ---------
Ethernet                  TAP-ProtonVPN Windows Adapter V9             32 Disconnected 00-FF-F9-EE-DD-E4         1 Gbps
VMware Network Adapte...1 VMware Virtual Ethernet Adapter for ...      30 Up           00-50-56-EE-DD-01       100 Mbps
Wi-Fi                     Dell Wireless 1820A 802.11ac                 25 Up           C8-FF-28-EE-DD-4D       702 Mbps
VMware Network Adapte...8 VMware Virtual Ethernet Adapter for ...      24 Up           00-50-56-EE-DD-08       100 Mbps
Bluetooth Network Conn... Bluetooth Device (Personal Area Netw...      15 Disconnected C8-FF-28-EE-DD-4E         3 Mbps
vEthernet (Default Swi... Hyper-V Virtual Ethernet Adapter             22 Up           00-15-5D-EE-DD-F0        10 Gbps
vEthernet (WSL)           Hyper-V Virtual Ethernet Adapter #2          38 Up           00-15-5D-EE-DD-00        10 Gbps
Run Code Online (Sandbox Code Playgroud)

Vom*_*yle 5

您可以使用get-netroute获取具有最低指标的0orfalse值的适配器索引号,因此该适配器应该是流量使用的默认值。

电源外壳

Get-NetRoute | % { Process { If (!$_.RouteMetric) { $_.ifIndex } } };
Set-DNSClientServerAddress –interfaceIndex $intix –ServerAddresses ("127.0.0.1","1.1.1.2");
Run Code Online (Sandbox Code Playgroud)

输出样本

命令: Get-NetRoute | Select InterfaceAlias, InterfaceIndex, RouteMetric | FL

InterfaceAlias : Loopback Pseudo-Interface 1
InterfaceIndex : 1
RouteMetric    : 256

InterfaceAlias : Wi-Fi 3
InterfaceIndex : 7
RouteMetric    : 0

InterfaceAlias : Local Area Connection* 12
InterfaceIndex : 14
RouteMetric    : 256

InterfaceAlias : Local Area Connection* 11
InterfaceIndex : 27
RouteMetric    : 256
Run Code Online (Sandbox Code Playgroud)

配套资源