powershell 中的代理配置

powershell 中的代理配置

我正在尝试在代理后面的 Windows 上安装 chocolatey:

@powershell -ExecutionPolicy unrestricted

在 power shell 中我正在执行

$wc=new-object net.webclient;
$wc.Proxy=new-object system.net.WebProxy('<myproxy-ip>:8012',$true);
$wc.Proxy.Credentials = new-object system.net.NetworkCredential('<myusername>','<mypass>');
iex ($wc.DownloadString('https://chocolatey.org/install.ps1'));

我收到以下错误

Exception calling "DownloadString" with "1" argument(s): "The remote server returned an error: (407) Proxy Authentication Required."
At line:1 char:1
+ iex ($wc.DownloadString('https://chocolatey.org/install.ps1'));
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WebException

我使用的用户名/密码与启动 Firefox/iexplorer 时必须输入的用户名/密码相同(见图)。代理没有配置默认用户名/密码,因为我必须始终输入它们。

在此处输入图片描述


更多细节(在 Firefox 的私人窗口中使用检查元素)

响应标头

Cache-Control: no-cache
Connection: close
Content-Length: 813
Content-Type: text/html; charset=utf-8
Pragma: no-cache
Proxy-Authenticate: BASIC realm="PROXY_INTERNET"
Proxy-Connection: close
Set-Cookie: BCSI-CS-dfaeac52a135c7c0=2; Path=/

答案1

https://github.com/chocolatey/chocolatey/wiki/Proxy-Settings-for-Chocolatey

在 powershell 中定义函数

function Create-Proxy($proxyHost,$proxyPort,$proxyUsername,$proxyPassword){
    #$proxy = [System.Net.WebRequest]::GetSystemWebProxy()
    $proxyUrl = $proxyHost+":"+$proxyPort;
    Write-Host "proxy url [$proxyUrl]";
    $proxy = New-Object System.Net.WebProxy($proxyUrl, $true);
    $passwd = ConvertTo-SecureString $proxyPassword -AsPlainText -Force; ## Website credentials
    $proxy.Credentials = New-Object System.Management.Automation.PSCredential ($proxyUsername, $passwd);
    return $proxy;
}

称之为

$wc=new-object net.webclient;
$wc.UseDefaultCredentials = $true
$wc.Proxy = Create-Proxy "<proxy-host>" "<proxy-port>" "<proxy-username>" "<proxy-clear-pass>"
$wc.DownloadString('https://chocolatey.org/install.ps1');

我发现实际的下载有效,但是安装程序的执行却无效,因为它使用了自定义构建的代理。

因此iex ($wc.DownloadString("https://chocolatey.org/install.ps1"));失败是因为下载的install.ps1

答案2

无法测试它(没有可用的类似代理),所以我实际上不知道这是否可行,但您可以尝试以下方法:

$wc = new-object net.webclient;
$proxyUri = new-object system.uri("http://<myproxy-ip>:8012");
$wc.Proxy = new-object system.net.WebProxy($proxyUri, $true);
$cachedCredentials = new-object system.net.CredentialCache;
$netCredential = new-object system.net.NetworkCredential("<myusername>", "<mypass>");

$cachedCredentials.Add($proxyUri, "Basic", $netCredential);

$wc.Proxy.Credentials = $cachedCredentials.GetCredential($proxyUri, "Basic");

iex ($wc.DownloadString("https://chocolatey.org/install.ps1"));

目的是使用 CredentialCache 对象强制凭证使用“基本”身份验证模式。

答案3

你可以用这个。对我有用。 https://github.com/chocolatey/choco/wiki/Proxy-Settings-for-Chocolatey

显式代理设置

Chocolatey 从 0.9.9.9 开始明确支持代理。

您只需配置 1 或 3 个设置,Chocolatey 就会使用代理服务器。proxy 是必需的,是代理服务器的位置和端口。proxyUser 和 proxyPassword 是可选的。用户/密码的值仅在两者存在时才用于凭据。

choco config set proxy <locationandport>
choco config set proxyUser <username>
choco config set proxyPassword <passwordThatGetsEncryptedInFile>

例子

在0.9.9.9中运行以下命令:

choco config set proxy http://localhost:8888
choco config set proxyUser bob
choco config set proxyPassword 123Sup#rSecur3

相关内容