【windows】【cmd】修改网络配置的 InterfaceMetric

使用脚本修改网卡跃点数 InterfaceMetric

使用到的接口

  1. Get-NetIPInterface
  2. Set-NetIPInterface
  3. Get-NetAdapter

需求描述

需要修改当前跃点数为 1 的ip接口,修改其跃点数为 90

解决方案

设置ip接口的跃点数均需要管理员权限

思路一

源文件

  1. 通过 Get-NetIPInterface 获取需要修改的IP接口
  2. 通过 Set-NetIPInterface 修改跃点数
1
2
3
4
5
6
7
8
9
10
11
12
13
$ChangeIfm = 1
$WangToChangeIfm = 90

# 获取 InterfaceMetric 为 1 的 -InterfaceIndex
$NetCard = (Get-NetIPInterface -InterfaceMetric $ChangeIfm).ifAlias
$netInterfaceIndex = (Get-NetIPInterface -AddressFamily IPv4 -InterfaceMetric $ChangeIfm).ifIndex

Write-Output "检索InterfaceMetric为 [${ChangeIfm}] 列表: `n ${NetCard}"
Write-Output "Index 为: ${netInterfaceIndex}"

Set-NetIPInterface -InterfaceIndex $netInterfaceIndex -InterfaceMetric $WangToChangeIfm

Write-Output "已修改 ${NetCard} 的 InterfaceMetric 为 ${WangToChangeIfm}"

思路二

源文件

使用过滤器筛选需要修改的ip接口,再通过 Set-NetIPInterface 修改跃点数

1
2
3
4
5
6
7
8

# 获取 InterfaceMetric 为 1 的 -InterfaceIndex
$NetCard = (Get-NetAdapter | Where-Object -FilterScript {$_.ifIndex -eq 10})

Write-Output $NetCard

Set-NetIPInterface -InterfaceIndex $NetCard.ifIndex -AddressFamily IPv6 -InterfaceMetric 90

官方demo

修改 LinkSpeed 为 100 Mbps 的网络的 InterfaceMetric 为 21

1
Get-NetAdapter | Where-Object -FilterScript {$_.LinkSpeed -Eq "100 Mbps"} | Set-NetIPInterface -InterfaceMetric 21

提升管理员权限

cmd提权

1
2
3
4
5
6
7
8
9
10
11
@echo off

::1 获取管理员权限
%1 mshta vbscript:CreateObject("Shell.Application").ShellExecute("cmd.exe","/c %~s0 ::","","runas",1)(window.close)&&exit

::2 执行操作
set curpath=%~dp0
set file=ModifyIfm.ps1
set execPath=%curpath%%file%
echo %execPath%
powershell.exe -ExecutionPolicy ByPass -File %execPath%

powershell 提权

1
2
3
4
5
6
7
if (-Not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')) {
if ([int](Get-CimInstance -Class Win32_OperatingSystem | Select-Object -ExpandProperty BuildNumber) -ge 6000) {
$Command = "-File `"" + $MyInvocation.MyCommand.Path + "`" " + $MyInvocation.UnboundArguments
Start-Process -FilePath PowerShell.exe -Verb RunAs -ArgumentList $Command
Exit
}
}

InterfaceMetric 是用于表示网络接口优先级的指标,它用于决定计算机在多个网络接口之间进行网络通信时所选择的默认接口。

在Windows操作系统中,InterfaceMetric是一个用于路由选择和接口优先级的整数值。较低的InterfaceMetric值表示较高的优先级,通常会被选择为默认接口。以下是InterfaceMetric的一些常用数值及其含义:

  • 0:最高优先级。当存在多个可用的接口时,操作系统将优先使用此接口进行网络通信。
  • 5:较高优先级。优先级稍低于0,但仍比较高,通常用于指定首选接口。
  • 10:默认优先级。如果没有特殊指定优先级的情况下,操作系统通常将选择InterfaceMetric为10的接口作为默认接口。
  • 大于10的值:较低优先级。这些值表示接口的优先级较低,只有在没有更高优先级的接口可用时才被选择。

请注意,InterfaceMetric值只在有多个可用的网络接口时才起作用。如果只有一个接口可用,则无论InterfaceMetric的值如何,该接口都将被选择为默认接口。

您可以通过以下步骤在Windows系统中修改接口的InterfaceMetric值:

  1. 打开网络连接设置:在Windows搜索栏中输入"控制面板",打开控制面板后选择"网络和 Internet",然后选择"网络和共享中心"。
  2. 打开适配器设置:在左侧面板中,点击"更改适配器设置"。
  3. 找到网络接口:在适配器设置窗口中,找到要修改InterfaceMetric的网络接口,右键点击该接口,然后选择"属性"。
  4. 修改接口属性:在接口属性窗口中,选择"Internet 协议版本 4 (TCP/IPv4)"(或其他适用的协议版本),然后点击"属性"按钮。
  5. 修改InterfaceMetric值:在该接口的属性窗口中,点击"高级"按钮,在"适配器度量"部分,可以手动输入想要设置的InterfaceMetric值(整数)。
  6. 保存设置:保存修改后的设置,然后关闭所有窗口。

注意:更改InterfaceMetric值后,可能需要重启计算机或者启用/禁用网络接口才能使更改生效。

希望这些信息能帮助您理解InterfaceMetric的含义及其在Windows系统中使用的方式。


【windows】【cmd】修改网络配置的 InterfaceMetric
https://hodlyounger.github.io/A_OS/Windows/cmd/修改ip接口的跃点数/
作者
mingming
发布于
2023年10月27日
许可协议