使用PowerShell从Jenkins下载工件

est*_*iec 4 powershell webclient jenkins

我尝试使用PowerShell从Jenkins下载工件,如下所示:

$webClient = new-object System.Net.WebClient 
$webClient.Credentials = New-Object System.Net.NetworkCredential ("username", "password")
$url = "http://jenkins/job/jobName/lastSuccessfulBuild/artifact/*zip*/archive.zip" 
$localfilename = "C:\Test\archive.zip"  
$webClient.DownloadFile($url, $localfilename)
Run Code Online (Sandbox Code Playgroud)

我得到例外:

使用"2"参数调用"DownloadFile"的异常:"远程服务器返回错误:(403)禁止." 在C:\ ps2.ps1:20 char:28 + $ webclient.DownloadFile <<<<($ url,$ localfilename)+ CategoryInfo:NotSpecified:(:) [],MethodInvocationException + FullyQualifiedErrorId:DotNetMethodException

如果我尝试使用wget下载工件,它可以工作:

wget --auth-no-challenge --http-user=username --http-password=password http://jenkins/job/jobName/lastSuccessfulBuild/artifact/*zip*/archive.zip
Run Code Online (Sandbox Code Playgroud)

如果我使用没有参数的wget,--auth-no-challenge我会得到同样的错误 - Forbidden.

est*_*iec 6

在请求标头中使用此授权非常有效:

# Create web client with authorization header
$webClient = new-object System.Net.WebClient 
$credentialAsBytes = [System.Text.Encoding]::ASCII.GetBytes($userName + ":" + $password)
$credentialAsBase64String = [System.Convert]::ToBase64String($credentialAsBytes);
$webClient.Headers[[System.Net.HttpRequestHeader]::Authorization] = "Basic " + $credentialAsBase64String;
Run Code Online (Sandbox Code Playgroud)