如何在Azure Powershell脚本任务中提供JSON请求正文

时间:2020-09-06 22:05:59

标签: powershell azure-devops

嗨,我正在使用Azure管道中的powershell任务运行REST API调用,如何将$ body值传递给以下API端点

Invoke-RestMethod -Uri $ uri -Method Post -ContentType“ application / json” -Headers $ header -Body $ body

以下请求正文的引用

enter image description here

1 个答案:

答案 0 :(得分:0)

似乎您正在尝试移动文件,请检查以下示例:

Param( 
[string]$organisation = "organisation", 
[string]$repositoryId = "f2aa1d87-xxxx-48df-xxxx-6505ffxxxxf9", 
[string]$keepForever = "true", 
[string]$user = "user", 
[string]$token = "token" )

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

$postresults = "https://dev.azure.com/$organisation/_apis/git/repositories/$repositoryId/pushes?api-version=6.0" 

 $body = '{
  "refUpdates": [
    {
      "name": "refs/heads/master",
      "oldObjectId": "6e3c1f07d12eaf805a16db1352771816148c24b5"
    }
  ],
  "commits": [
    {
      "comment": "Moving activetasks.md to a new folder.",
      "changes": [
        {
          "changeType": "rename",
          "sourceServerItem": "/activetasks.md",
          "item": {
            "path": "/tasks/content/activetasks.md"
          }
        }
      ]
    }
  ]
}'


$result = Invoke-RestMethod -Uri $postresults -Method Post -Body $body -ContentType "application/json" -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}