为什么需要设置代理?
在某些特殊环境下,公司拥有一个强大的防火墙,会主动阻断你使用github等网址,比如raw.githubusercontent.com
,很容易出现:
C:\Users\52905>ping raw.githubusercontent.com
Ping 请求找不到主机 raw.githubusercontent.com。请检查该名称,然后重试。
如何设置代理?
- 创建文件:C:\Users\你的用户名\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
- 打开刚刚创建的:Microsoft.PowerShell_profile.ps1
设置内容
Microsoft.PowerShell_profile.ps1文件的内容如下:
function proxy {
$env:http_proxy = "http://127.0.0.1:10809"
$env:https_proxy = "http://127.0.0.1:10809"
[System.Net.WebRequest]::DefaultWebProxy = New-Object System.Net.WebProxy("http://127.0.0.1:10809")
Write-Host "Proxy enabled: http://127.0.0.1:10809" -ForegroundColor Green
}
function unproxy {
$env:http_proxy = $null
$env:https_proxy = $null
[System.Net.WebRequest]::DefaultWebProxy = $null
Write-Host "Proxy disabled" -ForegroundColor Yellow
}
function check-proxy {
if ($env:http_proxy -or $env:https_proxy) {
Write-Host "Current proxy settings:" -ForegroundColor Cyan
Write-Host "HTTP Proxy: $env:http_proxy"
Write-Host "HTTPS Proxy: $env:https_proxy"
} else {
Write-Host "No proxy is currently set." -ForegroundColor Cyan
}
}
保存并重新加载配置
下面方法二选一
- 重新运行powershell命令行
- 运行
. $PROFILE
来重新加载配置文件。
如何使用
现在,你可以在 PowerShell 中使用以下命令:
- 输入 proxy 来启用代理
- 输入 unproxy 来禁用代理
- 输入 check-proxy 来查看当前的代理设置