AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1626047
Accepted
Jonesome Reinstate Monica
Jonesome Reinstate Monica
Asked: 2021-02-15 18:44:00 +0800 CST2021-02-15 18:44:00 +0800 CST 2021-02-15 18:44:00 +0800 CST

Powershell:如何找出公共接口的adapterIndex?

  • 772

我使用 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
powershell
  • 2 2 个回答
  • 5525 Views

2 个回答

  • Voted
  1. Vomit IT - Chunky Mess Style
    2021-02-15T18:59:35+08:002021-02-15T18:59:35+08:00

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

    电源外壳

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

    输出样本

    命令: 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
    

    支持资源

    • 获取网络路由
    • 设置 DnsClientServerAddress
    • ForEach-对象

      Foreach-Object 的标准别名%:' ' 符号,ForEach

    • 如果()
    • 5
  2. Best Answer
    swbbl
    2021-02-15T23:34:23+08:002021-02-15T23:34:23+08:00

    @Drink More Pimp Juice IT已经提到了一个简单的解决方案,如果only one network adapter启用并激活了默认网关,该解决方案就可以工作。

    如果您同时启用和激活了多个网络适配器,当您使用通过以太网连接时未禁用或断开 WiFi 适配器的笔记本电脑时会发生这种情况,您将有两条路由,其路由度量为0,但具有不同接口度量的。

    当手动配置路由时,默认路由的路由度量也可能根本不0存在。

    解决方案

    在这种情况下,如果要获取操作系统的全局默认路由,则必须取总和,RouteMetric并InterfaceMetric使用 IPv4 和 IPv6 的默认路由作为目标过滤器。

    以下命令使用 IPv4 和 IPv6 默认路由获取操作系统的默认路由。

    Get-NetRoute -DestinationPrefix '0.0.0.0/0', '::/0' |
        Sort-Object -Property { $_.InterfaceMetric + $_.RouteMetric } |
        Select-Object -First 1
    

    '::/0'如果您只想确定 IPv4 路由,则可以删除 IPv6 目标前缀。

    使用返回的接口索引ifIndex,您可以更改相应的网络适配器。也可以将上面的输出直接通过管道传输到Get-NetAdapter接受 InterfaceIndex 作为命名属性的任何其他 cmdlet。

    PowerShell 6+

    从 PowerShell 6 开始,我们可以简化命令,因为有新参数Top可用于Sort-Object.

    Get-NetRoute -DestinationPrefix '0.0.0.0/0', '::/0' |
        Sort-Object -Property { $_.InterfaceMetric + $_.RouteMetric } -Top 1 
    

    信息资源

    • 配置网络接口的顺序
      https://docs.microsoft.com/en-us/windows-server/networking/technologies/network-subsystem/net-sub-interface-metric

      When network traffic routes are chosen and you have configured the InterfaceMetric parameter of the Set-NetIPInterface command, the overall metric that is used to determine the interface preference is the sum of the route metric and the interface metric . 通常,接口度量优先于特定接口,例如,如果有线和无线都可用,则使用有线。

    • IPv4 路由的自动度量功能的说明
      https://docs.microsoft.com/en-us/troubleshoot/windows-server/networking/automatic-metric-for-ipv4-routes

    • 4

相关问题

  • 如何将变量字符串放入powershell中的数组?

  • Powershell 和正则表达式:Notepad++“保存时备份”文件列表。编辑名称,按上次写入时间排序

  • 将前景颜色添加到 Powershell 配置文件?

  • 禁用后无法启用 Microsoft Print to PDF

  • 我可以让这个 PowerShell 脚本接受逗号吗?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    Saaru Lindestøkke 为什么使用 Python 的 tar 库时 tar.xz 文件比 macOS tar 小 15 倍? 2021-03-14 09:37:48 +0800 CST
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    v15 为什么通过电缆(同轴电缆)的千兆位/秒 Internet 连接不能像光纤一样提供对称速度? 2020-01-25 08:53:31 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve