G
N
I
D
A
O
L

修改ip接口的跃点数

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

使用到的接口

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

需求描述

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

解决方案

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

思路一

源文件

  1. 通过 Get-NetIPInterface 获取需要修改的IP接口
  2. 通过 Set-NetIPInterface 修改跃点数
$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 修改跃点数


# 获取 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

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

提升管理员权限

cmd提权

@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 提权

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
 }
}
posted @ 2023-06-06 16:50  StimuMing  阅读(280)  评论(0编辑  收藏  举报