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

Downloading ZIP content through REST API call in Powershell

这是一个示例 REST API uri,用于从 AzureDevOps 版本中检索日志。 https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/releases/{releaseId}/logs?api-version=4.1-preview.2

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

如何使用 Invoke-RestMethod 通过 powershell 通过 REST API 调用检索 zip 文件?

如果我将 Out-File 传递给此命令并将其另存为 zip,它不会将 API 响应的二进制输出转换为 zip。

我该怎么做?

您需要将输出命名为 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