You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell双网卡优先级切换脚本:Test-Connection参数报错求助

问题修复与替代方案

原因分析

Test-Connection的-InterfaceAlias参数仅在**PowerShell 7+(Core版本)**中可用,如果你使用的是Windows默认的PowerShell 5.1,就会触发参数不存在的报错。

替代方案:指定源IP执行网络检测

我们可以先获取对应网卡的IPv4地址,通过指定源IP的方式强制使用目标网卡执行连通性检测,以下提供两种兼容PowerShell 5.1的实现方式:

方式1:使用ping.exe命令(兼容性最优)

通过Get-NetIPAddress获取网卡的IPv4地址,调用系统原生ping.exe并指定源IP,最后通过返回码判断连通状态。修改后的完整脚本如下:

function Set-InternetPriority {
    param (
        [string]$EthernetAdapterName,
        [string]$WiFiAdapterName
    )

    # 获取以太网适配器的IPv4地址
    $ethernetIP = (Get-NetIPAddress -InterfaceAlias $EthernetAdapterName -AddressFamily IPv4 | Select-Object -First 1).IPAddress
    # 获取Wi-Fi适配器的IPv4地址
    $wifiIP = (Get-NetIPAddress -InterfaceAlias $WiFiAdapterName -AddressFamily IPv4 | Select-Object -First 1).IPAddress

    # 检测以太网联网状态
    $ethernetPingResult = $false
    if ($ethernetIP) {
        ping.exe -S $ethernetIP 8.8.8.8 -n 1 -w 1000 | Out-Null
        $ethernetPingResult = ($LASTEXITCODE -eq 0)
    } else {
        Write-Warning "以太网适配器未获取到有效IPv4地址"
    }

    # 检测Wi-Fi联网状态
    $wifiPingResult = $false
    if ($wifiIP) {
        ping.exe -S $wifiIP 8.8.8.8 -n 1 -w 1000 | Out-Null
        $wifiPingResult = ($LASTEXITCODE -eq 0)
    } else {
        Write-Warning "Wi-Fi适配器未获取到有效IPv4地址"
    }

    # 根据检测结果调整适配器跃点
    if ($ethernetPingResult -and $wifiPingResult) {
        Write-Output "以太网和Wi-Fi均联网,设置以太网为默认"
        Set-NetIPInterface -InterfaceAlias $EthernetAdapterName -InterfaceMetric 10
        Set-NetIPInterface -InterfaceAlias $WiFiAdapterName -InterfaceMetric 20
    }
    elseif ($ethernetPingResult) {
        Write-Output "仅以太网联网,保持以太网默认"
        Set-NetIPInterface -InterfaceAlias $EthernetAdapterName -InterfaceMetric 10
        Set-NetIPInterface -InterfaceAlias $WiFiAdapterName -InterfaceMetric 100
    }
    elseif ($wifiPingResult) {
        Write-Output "仅Wi-Fi联网,切换为Wi-Fi默认"
        Set-NetIPInterface -InterfaceAlias $EthernetAdapterName -InterfaceMetric 100
        Set-NetIPInterface -InterfaceAlias $WiFiAdapterName -InterfaceMetric 10
    }
    else {
        Write-Output "两者均未联网,恢复默认跃点"
        Set-NetIPInterface -InterfaceAlias $EthernetAdapterName -InterfaceMetric 10
        Set-NetIPInterface -InterfaceAlias $WiFiAdapterName -InterfaceMetric 20
    }
}

# 每60秒循环执行检测与适配
while ($true) {
    Set-InternetPriority -EthernetAdapterName "Ethernet" -WifiAdapterName "Wi-Fi"
    Start-Sleep -Seconds 60
}

方式2:使用Test-NetConnection命令

Test-NetConnection支持-SourceAddress参数,可指定源IP完成连通性检测,替换脚本中检测部分的代码如下:

# 检测以太网联网状态
$ethernetPingResult = $false
if ($ethernetIP) {
    $ethernetPingResult = (Test-NetConnection -ComputerName 8.8.8.8 -SourceAddress $ethernetIP -InformationLevel Quiet)
}

# 检测Wi-Fi联网状态
$wifiPingResult = $false
if ($wifiIP) {
    $wifiPingResult = (Test-NetConnection -ComputerName 8.8.8.8 -SourceAddress $wifiIP -InformationLevel Quiet)
}

额外注意事项

  • 必须以管理员权限运行脚本,否则Set-NetIPInterface会因权限不足无法修改适配器跃点。
  • 若你的网卡名称不是默认的"Ethernet"或"Wi-Fi",请替换为实际网卡别名(可通过Get-NetAdapter命令查看)。
  • 可根据网络环境调整ping的超时时间(ping.exe的-w参数,单位毫秒)和测试包数量(-n参数)。

内容的提问来源于stack exchange,提问作者Tastic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 08:29:57