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

bit*_*tes 4 rest powershell azure-devops azure-devops-rest-api

这是一个示例 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”

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

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

我该怎么做 ?

And*_*SFT 6

您需要将输出命名为 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
Run Code Online (Sandbox Code Playgroud)