. './listBuilds.ps1' . './listArtifacts.ps1' $token = 'token' $instance = 'dev.azure.com/myorganization' $project = 'myproject' $branches = 'V1.0.0.0', 'V1.0.0.1' $outDir = "$PSScriptRoot\output" $headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" $headers.Add("Authorization", "Basic ${token}") # Remove outDir if (Test-Path $outDir) { Remove-Item -Recurse $outDir } # Get the list of builds in a repo $builds = ListBuilds -token $token -instance $instance -project $project ForEach ($branch in $branches) { # Select only the last build for the branches you want $lastBuild = $builds.value | Where-Object { $_.sourceBranch -eq "refs/heads/$branch"} | Select-Object -First 1 # Get the list of artifacts for each build $listArtifacts = ListArtifacts -token $token -instance $instance -project $project -buildId $lastBuild.id $artifacts = $listArtifacts.value # Create output directory $outputDir = "$outDir\$branch" New-Item -Path $outDir -Name $branch -ItemType Directory # Download the artifacts ForEach ($artifact in $artifacts) { $artifactName = $artifact.name $outputFile = "$outputDir\$artifactName.zip" Invoke-RestMethod -Uri $artifact.resource.downloadUrl -Method 'GET' -Headers $headers -OutFile $outputFile # Unzip Expand-Archive $outputFile -DestinationPath $outputDir } }