Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 680
bits-bytes Avatar asked Oct 26 '25 08:10

bits-bytes


1 Answers

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
like image 149
Andy Li-MSFT Avatar answered Oct 29 '25 06:10

Andy Li-MSFT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!