Download artifact from Jenkins using PowerShell
jenkins, powershell, webclient
Solution
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;
Problem
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`.