Created
May 10, 2024 04:04
-
-
Save pokon548/12c70736159d00e5b16a5e48801e1927 to your computer and use it in GitHub Desktop.
一键切换网络优先级
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Requires -RunAsAdministrator | |
Add-Type -AssemblyName System.Windows.Forms | |
function Read-MessageBoxDialog | |
{ | |
param ([string]$Message, | |
[string]$WindowTitle, | |
[System.Windows.Forms.MessageBoxButtons]$Buttons = [System.Windows.Forms.MessageBoxButtons]::OK, | |
[System.Windows.Forms.MessageBoxIcon]$Icon = [System.Windows.Forms.MessageBoxIcon]::None) | |
Add-Type -AssemblyName System.Windows.Forms | |
return [System.Windows.Forms.MessageBox]::Show($Message, $WindowTitle, $Buttons, $Icon) | |
} | |
$interface=Get-NetRoute | Where-Object -FilterScript {$_.NextHop -Ne "::"} | Where-Object -FilterScript { $_.NextHop -Ne "0.0.0.0" } | Where-Object -FilterScript { ($_.NextHop.SubString(0,6) -Ne "fe80::") } | Get-NetIPInterface | Select-Object -Unique | Where-Object -FilterScript { $_.ConnectionState -Ne "Disconnected" } | Sort-Object -Property InterfaceMetric | |
$ifIndex=$interface | Select-Object -ExpandProperty "ifIndex" | |
$InterfaceMetric=$interface | Select-Object -ExpandProperty "InterfaceMetric" | |
$currentConnection=$interface[0] | Select-Object -ExpandProperty "InterfaceAlias" | |
$first, $availableConnection= $interface | Select-Object -ExpandProperty "InterfaceAlias" | |
$choice=Read-MessageBoxDialog -Message "Windows 目前优先使用的网络连接是:$($currentConnection)。可以切换到这些网络连接:$($availableConnection)`n`n是否要进行切换,以优先使用其它网络连接?" -WindowTitle "网络优先级切换实用小工具" -Buttons YesNo -Icon Information | |
if ($choice -eq "Yes") | |
{ | |
[array]::Reverse($ifIndex) | |
foreach ($if in $ifIndex) | |
{ | |
Set-NetIPInterface -InterfaceIndex $if -InterfaceMetric ([array]::IndexOf($ifIndex, $if)) | |
} | |
Read-MessageBoxDialog -Message "切换完成。修改将在片刻后生效" -WindowTitle "网络优先级切换实用小工具" -Buttons Ok -Icon Information | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment