水个贴,分享下自己在用的自动设置 windows 终端 powershell 代理的启动脚本,内容写入 C:\Users\$user\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1 文件中即可,记得替换 $user 为自己用户名。
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$regPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
$proxyEnable = Get-ItemProperty -Path $regPath -Name ProxyEnable -ErrorAction SilentlyContinue
$proxyServer = Get-ItemProperty -Path $regPath -Name ProxyServer -ErrorAction SilentlyContinue
if ($proxyEnable -and ($proxyEnable.ProxyEnable -eq 1) -and $proxyServer) {
$proxy = "http://$($proxyServer.ProxyServer)"
$env:HTTP_PROXY = $proxy
$env:HTTPS_PROXY = $proxy
Write-Host "系统代理已启用: $proxy"
} else {
$env:HTTP_PROXY = $null
$env:HTTPS_PROXY = $null
Write-Host "系统代理未启用"
}
在使用 启用系统代理 而不是 tun 的时候比较方便,能自动判断是否设置了系统代理并设置终端环境变量。使用 wsl 时还是推荐 tun 模式,简单省事。