I need to download artifacts from azure pipeline on my local machine. Can anyone help in doing this using powershell script?
-
Not get your latest information, is my workaround helpful for you? Or if you have any concern, feel free to share it here. – Hugh Lin Sep 17 '20 at 01:56
-
I used your Suggested method to convert my PAT token to Base64 then i used that in creating Authorization header which is used in Invoke-Restmethod. Thanks for helping I got my solution. – Tabrez Shams Sep 17 '20 at 05:16
3 Answers
The solutions provided above do not or no longer work - the API seems to have changed. If you use the solutions you get HTML content asking you to sign in.
The new API returns JSON with a downloadUrl:
{
"id": 105284,
"name": "Artifact",
"resource": {
...
"downloadUrl": "https://artprodsu6weu.artifacts.visualstudio.com/....."
}
}
So here is some code which works fine:
$BuildId = "154782"
$ArtifactName = "Artifact"
$OutFile = "Artifact.zip"
$OrgName="myorg"
$ProjectName="myproject"
$PAT = "**************"
$url = "https://dev.azure.com/$($OrgName)/$($ProjectName)/_apis/build/builds/$($BuildID)/artifacts?artifactName=$($ArtifactName)&api-version=&format=zip"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PAT)"))
# Get the download URL
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"}
$downloadUrl = $response.resource.downloadUrl
# Download the artifact
$response = Invoke-WebRequest -Uri $downloadUrl -Headers @{Authorization = "Basic $token"} -OutFile $OutFile

- 13,011
- 11
- 78
- 98
-
1Very helpful! BTW, the commas after the first two variable declarations should be removed. I tried to edit them out here, but SO said an edit had to be at least 6 characters. – Bill Menees Jun 14 '21 at 21:07
$token = "xxx"
$url="https://dev.azure.com/{OrgName}/{ProjectName}/_apis/build/builds/{BuildID}/artifacts?artifactName={ArtifactName}&api-version=6.1-preview.5&%24format=zip"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Get -ContentType application/zip -OutFile "{SomePath}\Response.zip"
Note: Add &%24format=zip
after the url and set -ContentType application/zip -OutFile "{SomePath}\Response.zip"
You need to replace token(PAT),OrgName,ProjectName,BuildID,ArtifactName
with your own values. And choose one existing path to save the response, such as C:\pub\Response.zip
. I have existing path C:\pub
, after running the PS script I can get one created Response.zip
which contains the artifact I need.
In addition , you can also try to download the build artifact through c# code. For details,please refer to this ticket.
static readonly string TFUrl = "https://dev.azure.com/OrgName/";
static readonly string UserPAT = "PAT";
static void Main(string[] args)
{
try
{
int buildId = xx; // update to an existing build definition id
string artifactName = "drop"; //default artifact name
// string project = "projectName";
ConnectWithPAT(TFUrl, UserPAT);
Stream zipStream = BuildClient.GetArtifactContentZipAsync(buildId, artifactName).Result; //get content
using (FileStream zipFile = new FileStream(@"C:\MySite\test.zip", FileMode.Create))
zipStream.CopyTo(zipFile);
Console.WriteLine("Done");
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.Message);
if (ex.InnerException != null) Console.WriteLine("Detailed Info: " + ex.InnerException.Message);
Console.WriteLine("Stack:\n" + ex.StackTrace);
}
}

- 17,829
- 2
- 21
- 25
-
-
Why do you have `%24` in your URL? Should it not be `&api-version=6.1-preview.5&format=zip`? – Marc May 11 '21 at 13:47
You can use the Artifacts Rest API:
$token = "Your PAT"
# Create Authorization header
$headers = @{"Authorization" = "Bearer $token"}
# Create Web client - used to downlaod files
$wc = New-Object System.Net.WebClient
$wc.Headers.Add("Authorization", $headers["Authorization"])
# Get Build artifact details
$buildId = "the artifats build id"
$artifactsUrl = "https://dev.azure.com/{organization}/{project}/_apis/build/builds/$buildId/artifacts?api-version=4.1"
$buildArtifacts = Invoke-RestMethod -Method Get -Headers $headers -Uri $artifactsUrl
foreach($buildArtifact in $buildArtifacts.value){
# Download build artifacts - ZIP files
$url = $buildArtifact.resource.downloadUrl
$output = Join-Path $artifactsDir "$($buildArtifact.name).zip"
$wc.DownloadFile($url, $output)
}
$wc.Dispose()

- 36,824
- 16
- 89
- 114
-
can you try to debug the lines with `Write-Host` and check which line doesn't work? – Shayki Abramczyk Sep 03 '20 at 13:38
-
did you replace the values in `{organization}` & `{project}` with your project details and put value in `$buildId`? – Shayki Abramczyk Sep 04 '20 at 14:13
-