【问题标题】:Downloading ZIP content through REST API call in Powershell在 Powershell 中通过 REST API 调用下载 ZIP 内容
【发布时间】:2019-03-27 23:07:18
【问题描述】:

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

我该怎么做?

【问题讨论】:

  • API 的二进制输出是什么?它是十六进制字符串还是字节数组?它是base64编码的吗?这个想法是,您需要获取任何响应,将其转换为字节数组,然后将其发送到Set-Content -Encoding byte[System.IO.File]::WriteAllBytes($FullPathToOutputFile, $ByteArray)

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


【解决方案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

【讨论】:

  • 完美运行。我使用的是`| Out-File -filepath C:\Test1\logs.zip' 不起作用。非常感谢!
猜你喜欢
  • 1970-01-01
  • 2020-01-21
  • 1970-01-01
  • 1970-01-01
  • 2021-07-01
  • 2014-12-24
  • 1970-01-01
  • 1970-01-01
  • 2021-09-10
相关资源
最近更新 更多