使用PowerShell从Jenkins下载工件

时间:2014-01-29 09:22:35

标签: powershell jenkins webclient

我尝试使用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)

我得到例外:

  

使用“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

如果我使用不带参数--auth-no-challenge的wget,我会收到同样的错误 - Forbidden

4 个答案:

答案 0 :(得分: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;

答案 1 :(得分:2)

您是否在Jenkins盒子上设置了基本身份验证?如果是这样,试试这个

$webClient = new-object System.Net.WebClient 
$creds = New-Object System.Net.NetworkCredential ("username", "password")
$url = "http://jenkins/job/jobName/lastSuccessfulBuild/artifact/*zip*/archive.zip" 
$localfilename = "C:\Test\archive.zip"  
$cache = new-object System.Net.CredentialCache
$cache.Add($url, "Basic", $creds)
$webclient.Credentials = $cache
$webClient.DownloadFile($url, $localfilename)

答案 2 :(得分:0)

我还没有在Jenkins上试过这个,但这就是我用来下载附加到Jira(https)中的问题的文件:

Invoke-WebRequest -uri "https://jira.xyz.com/login.jsp?os_username=$username&os_password=$jiraPassword&os_cookie=true" -UserAgent ([Microsoft.PowerShell.Commands.PSUserAgent]::InternetExplorer) -SessionVariable myWebSession

Invoke-WebRequest -uri $fileUrlPath -LocalOutFilePath $file -method get -UserAgent ([Microsoft.PowerShell.Commands.PSUserAgent]::InternetExplorer) -WebSession $myWebSession

答案 3 :(得分:0)

自 Jenkins 2.0 起,clipBehaviour 需要提供会话 cookie。在这种情况下,您需要额外的步骤:

$crumbParams = @{
    Headers = @{'Authorization' = 'Basic ' + [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("$($credentials.UserName):$($credentials.GetNetworkCredential().Password)"))}
    Uri = "$baseUri/crumbIssuer/api/json"
    SessionVariable = 'crumbSession'
}
[void] (Invoke-RestMethod @crumbParams)

请注意,如果您提供带有域的凭据,如果 Jenkins 只需要用户名而不需要域,则您可能必须使用 $credentials.GetNetworkCredential().UserName$baseUri 是 Jenkins 地址,包括 http(s)://

前面的命令会将会话状态保存在 $crumbSession 中,您可以从中提取所需的 cookie 标头:

$webClient = New-Object System.Net.WebClient
$webClient.Headers.Add([System.Net.HttpRequestHeader]::Cookie, $crumbSession.Cookies.GetCookieHeader($baseUri))
$webClient.Headers.Add([System.Net.HttpRequestHeader]::Authorization, 'Basic ' + [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("$($credentials.UserName):$($credentials.GetNetworkCredential().Password)")))
$webClient.DownloadFile($url, $localfilename)