Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download artifact from Jenkins using PowerShell

I try download artifact from Jenkins using PowerShell, like this:

$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)

I get exception:

Exception calling "DownloadFile" with "2" argument(s): "The remote server retur ned an error: (403) Forbidden." At C:\ps2.ps1:20 char:28 + $webclient.DownloadFile <<<< ($url, $localfilename) + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : DotNetMethodException

If I try download artifact using wget it works:

wget --auth-no-challenge --http-user=username --http-password=password http://jenkins/job/jobName/lastSuccessfulBuild/artifact/*zip*/archive.zip

If I using wget without parameter --auth-no-challenge I get the same error - Forbidden.

like image 482
estradowiec Avatar asked Jan 29 '14 09:01

estradowiec


People also ask

Does Jenkins support PowerShell?

Jenkins PowerShell PluginIntegrates with PowerShell by allowing you to directly write PowerShell scripts into the text box in Jenkins. Other than that, this plugin works pretty much like the standard shell script support.


1 Answers

Using this authorization in request header is working great:

# 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;
like image 138
estradowiec Avatar answered Sep 22 '22 09:09

estradowiec