bits-bytes
bits-bytes

Reputation: 77

Downloading ZIP content through REST API call in Powershell

This is a sample REST API uri to retrieve logs from the AzureDevOps Release. https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/releases/{releaseId}/logs?api-version=4.1-preview.2

The Verb is GET and content type is "application/zip"

How do i retrieve the zip file through a REST API call through powershell using Invoke-RestMethod ?

If i pass Out-File to this command and save it as zip, it doesn't convert the binary output of the API response to zip.

How do i do it ?

Upvotes: 4

Views: 7164

Answers (1)

Andy Li-MSFT
Andy Li-MSFT

Reputation: 30392

You need to name the output as zip.

Below Powershell script works for me:

Param(
   [string]$collectionUrl = "https://vsrm.dev.azure.com/{organization}",
   [string]$project = "0522TFVCScrum",
   [string]$releaseid = "35",
   [string]$filename = "D:\temp\ReleaseLogs_$releaseid.zip",
   [string]$user = "username",
   [string]$token = "password/PAT"
)

# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

$uri = "$collectionUrl/$project/_apis/Release/releases/$releaseid/logs"

Invoke-RestMethod -Uri $uri -Method Get -ContentType "application/zip" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -OutFile $filename

Upvotes: 6

Related Questions