PowerShell中使用代理服务
在PowerShell中使用Update-Help
Invoke-WebRequest https://woshub.com
Find-Module modulename
等网络请求命令的时候会出现无法连接到服务器的错误,这时我们可以使用代理服务来解决这个问题。
-
检查当前系统代理设置
netsh winhttp show proxy
-
设置代理服务
# 设置代理的两种方式 # 1. 导入windows(Internet Explorer)中的代理设置 netsh winhttp import proxy source=ie # 2. 或者手动设置代理 netsh winhttp set proxy "127.0.0.1:8889" # 2.1 设置的时候可以指定IP地址或网站不使用代理(设置bypass-list) netsh winhttp set proxy "127.0.0.1:8889" bypass-list="10.*,172.*,192.168.*,*.corp.woshub.com" # 2.2 可以通过下面的命令检查指定的URL是否通过代理连接 ([System.Net.WebRequest]::GetSystemWebproxy()).IsBypassed("https://woshub.com")
-
认证代理服务器
针对连接代理服务器需要认证,有两种方式:一种是使用Windows SSO,另一种是指定用户认证证书# 1. 使用Windows SSO $Wcl=New-Object System.Net.WebClient $Wcl.Headers.Add("user-agent", "PowerShell Script") $Wcl.Proxy.Credentials= System.Net.CredenticalCache]::DefaultNetworkCredentials # 2. 指定用户认证证书 $Wcl=New-Object System.Net.WebClient $Creds=Get-Credential $Wcl.Proxy.Credentials=$Creds
-
为PowerShell Core配置代理连接
在新版本的PowerShell Core(6.x, 7.x)中使用System.Net.HttpClient
类代替了原来的System.Net.WebRequest
类来执行网络请求,因此配置命令变成如下:# 设置代理 [System.Net.Http.HttpClient]::DefaultProxy = New-Object System.Net.WebProxy('http://your-proxy:3128') # 设置代理服务器认证 [System.Net.Http.HttpClient]::DefaultProxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials [System.Net.Http.HttpClient]::DefaultProxy.Credentials = Get-Credential
-
PowerShell Core还支持通过特定的Windows环境变量来设置代理
$proxy='http://127.0.0.1:8889' $ENV:HTTP_PROXY=$proxy $ENV:HTTPS_PROXY=$proxy # 带认证的proxy(不安全) $proxy='http://username:password@127.0.0.1:8889' # 检查代理环境变量 Dir env:
-
在PowerShell配置文件中设置代理
将下面的内容存入的PowerShell的配置文件中# Force PowerShell to use TLS 1.2 for connections [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 [system.net.webrequest]::DefaultWebProxy = new-object system.net.webproxy('http://127.0.0.1:8889') # If you need to import proxy settings from Internet Explorer, you can replace the previous line with the: "netsh winhttp import proxy source=ie" [system.net.webrequest]::DefaultWebProxy.credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials # You can request user credentials: # System.Net.WebRequest]::DefaultWebProxy.Credentials = Get-Credential # Also, you can get the user password from a saved XML file (see the article “Using saved credentials in PowerShell scripts”): # System.Net.WebRequest]::DefaultWebProxy= Import-Clixml -Path C:\PS\user_creds.xml [system.net.webrequest]::DefaultWebProxy.BypassProxyOnLocal = $true
默认情况下PowerShell脚本的执行策略是不允许执行脚本,需要进行下面的设置
Set-ExecutionPolicy RemoteSigned
最后保存配置文件,重启PowerShell终端。
-
在当前PowerShell中查看代理服务设置
Get-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' | Select-Object ProxyServer, ProxyEnable # 获取WebProxy设置 [System.Net.WebProxy]::GetDefaultProxy() # 开启代理 Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' ProxyEnable -value 1 # 关闭代理 Set-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings' ProxyEnable -value 0
-
windows sublinux - ubuntu配置代理
#!/bin/bash nameserver=`cat /etc/resolv.conf | grep nameserver | awk '{print $2}'` #echo "nameserver: ${nameserver}" export HTTP_PROXY=http://${nameserver}:8889 #echo "http proxy: ${HTTP_PROXY}" export HTTPS_PROXY=${HTTP_PROXY} #echo "https proxy: ${HTTPS_PROXY}" aptproxy="Acquire::http::Proxy \"http://${nameserver}:8889\";" #echo "apt proxy: ${aptproxy}" echo ${aptproxy} > /etc/apt/apt.conf.d/proxy.conf # hostname -I # 获取IP地址