阿杰i
开玩笑那 你这严重超纲了

根据改变跃点来切换有线网络与无线网络连接,体验较好,无需关闭适配器,但是有的时候并不好用,实在是没查到这个自动跃点还有啥机制🤔
- 有线网络名与无线网络名要自己根据自己电脑的配置,没自动查是因为有的时候会有多个。尤其是有虚拟机的时候
- 运行方式可以配置到 快捷命令 插件中,以powershell运行,忽略输出
- 也可以保存到xx.ps1脚本中, 新建一个x.bat 内容为 powershell /path/to/xx.ps1,然后将x.bat添加到utools中
param( $mode )
# 定义有线网络与无线网络适配器名称 区分大小写
$WiredNetworkName = "以太网 2";
$WirelessNetworkName = "WLAN";
# 以管理员身份运行
$currentWi = [Security.Principal.WindowsIdentity]::GetCurrent()
$currentWp = [Security.Principal.WindowsPrincipal]$currentWi
if( -not $currentWp.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
{
$boundPara = ($MyInvocation.BoundParameters.Keys | foreach{
'-{0} {1}' -f $_ ,$MyInvocation.BoundParameters[$_]} ) -join ' '
$currentFile = (Resolve-Path $MyInvocation.InvocationName).Path
$fullPara = $boundPara + ' ' + $args -join ' '
Start-Process "$psHome\powershell.exe" -ArgumentList "$currentFile $fullPara" -verb runas
return
}
# 获取有线网络跃点及索引
$WiredNetwork = Get-NetIPInterface | ? {$_.AddressFamily -eq "IPv4"} | ? {$_.InterfaceAlias -eq $WiredNetworkName} | select -Property ifIndex, InterfaceMetric, ConnectionState;
# 获取无线网络跃点及索引;
$WirelessNetwork = Get-NetIPInterface | ? {$_.AddressFamily -eq "IPv4"} | ? {$_.InterfaceAlias -eq $WirelessNetworkName} | select -Property ifIndex, InterfaceMetric, ConnectionState;
# 定义通知函数
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms");
function ShowNotify($title, $content, $ico) {
$obj = New-Object System.Windows.Forms.NotifyIcon;
$path = Get-Process -id $pid | Select-Object -ExpandProperty Path;
$icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path);
$obj.Icon = $icon;
$obj.BalloonTipIcon = $ico;
$obj.BalloonTipText = $content;
$obj.BalloonTipTitle = $title;
$obj.Visible = $True;
$obj.ShowBalloonTip(10000);
}
# 重置模式 重置跃点为自动模式
if ($mode -eq "reset") {
if($WiredNetwork) {
Set-NetIPInterface -InterfaceIndex $WiredNetwork.ifIndex -AutomaticMetric Enabled
}
if($WirelessNetwork) {
Set-NetIPInterface -InterfaceIndex $WirelessNetwork.ifIndex -AutomaticMetric Enabled
}
ShowNotify '提示' '网络连接行为已重置为默认状态' 'Info';
exit;
}
# 调换跃点
if ($WiredNetwork.ConnectionState -eq "Connected" -and $WirelessNetwork.ConnectionState -eq "Connected") {
$w = $WiredNetwork.InterfaceMetric;
$wl = $WirelessNetwork.InterfaceMetric;
if ($w -lt $wl) {
# Set-NetIPInterface -InterfaceIndex $WirelessNetwork.ifIndex -InterfaceMetric 11
# Set-NetIPInterface -InterfaceIndex $WiredNetwork.ifIndex -InterfaceMetric 24
Set-NetIPInterface -InterfaceIndex $WirelessNetwork.ifIndex -InterfaceMetric ($w + 1)
Set-NetIPInterface -InterfaceIndex $WiredNetwork.ifIndex -InterfaceMetric $wl
Set-NetIPInterface -InterfaceIndex $WirelessNetwork.ifIndex -InterfaceMetric $w
} else {
# Set-NetIPInterface -InterfaceIndex $WiredNetwork.ifIndex -InterfaceMetric 11
# Set-NetIPInterface -InterfaceIndex $WirelessNetwork.ifIndex -InterfaceMetric 24
Set-NetIPInterface -InterfaceIndex $WiredNetwork.ifIndex -InterfaceMetric ($wl + 1)
Set-NetIPInterface -InterfaceIndex $WirelessNetwork.ifIndex -InterfaceMetric $w
Set-NetIPInterface -InterfaceIndex $WiredNetwork.ifIndex -InterfaceMetric $wl
}
ShowNotify '提示' '网络切换成功' 'Info';
} else {
ShowNotify '警告' '当前只有单一网络连接,无法切换' 'Warning';
exit;
}