通过Powershell中的REST API调用下载ZIP内容

时间:2018-10-23 16:17:19

标签: rest powershell azure-devops azure-devops-rest-api

这是一个示例REST API uri,用于从AzureDevOps版本中检索日志。  "

动词是GET,内容类型是“ application / zip”

如何使用)通过Powershell通过REST API调用来检索zip文件?

如果我将https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/releases/{releaseId}/logs?api-version=4.1-preview.2传递给此命令并将其另存为zip,则不会将API响应的二进制输出转换为zip。

我该怎么做?

1 个答案:

答案 0 :(得分:1)

您需要将输出命名为zip。

下面的Powershell脚本对我有用:

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