PowerShell脚本中的Azure DevOps REST API TOP参数不起作用 [英] Azure DevOps REST API top parameter in PowerShell script is not working

查看:0
本文介绍了PowerShell脚本中的Azure DevOps REST API TOP参数不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下脚本可以从Azure DevOps项目生成发布报告。

$token="**************************************************************"

$url="https://dev.azure.com/{orgnization}/_apis/projects?api-version=6.0"

$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))



$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Get -ContentType application/json

Foreach($projectName in $response.value.name)
{
  
  $url1="https://vsrm.dev.azure.com/{orgnization}/$($projectname)/_apis/release/releases?api-version=6.0"
 
  $response = Invoke-RestMethod -Uri $url1 -Headers @{Authorization = "Basic $token"} -Method Get -ContentType application/json-patch

  
   echo $response | ConvertTo-Json -Depth 99 |  Out-File "D:\file.json" -Append

}

在此脚本中,第一个API仅返回100条记录。当我尝试添加top参数以返回更多记录(如下所示)时,它没有返回任何内容。我是不是做错了什么?

https://dev.azure.com/uniperteamservices/_apis/projects?api-version=6.0&$top=500

您能建议如何在REST API url中添加可以在上面的PowerShell脚本中运行的top参数吗?

推荐答案

务必转义查询字符串中的$,否则PowerShell将尝试注入任何名为topHold的变量的值:

$urlBase="...?api-version=6.0&`$top=5"
                              ^^^^^

$top=有时很棘手。它可能会给你,也可能不会给你所要求的项目数量,这似乎取决于后端有多忙。但它确实向您承诺了这一点:所有的rest API都将在有效负载旁边返回一个x-ms-continuation-token头,您可以使用它来获取下一批项:

您可以通过请求完全相同的REST查询并将&continuationtoken=${ms-continuation-token}查询参数添加到调用来获取下一批项目。重复此操作,直到服务器停止发送x-ms-continuation-token标头,这表示您已获得所需的所有值。

您可以通过传入第二个变量来获取标头:

$urlBase="https://dev.azure.com/jessehouwing/_apis/projects?api-version=6.0&`$top=5"

$url = $urlBase
$results = @();

do 
{
    write-host "Calling API"
    $response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Get -ContentType application/json -ResponseHeadersVariable headers
    $results += $response.value

    if ($headers["x-ms-continuationtoken"])
    {
        $continuation = $headers["x-ms-continuationtoken"]
        write-host "Token: $continuation"
        $url = $urlBase + "&continuationtoken=" + $continuation
    }
} while ($headers["x-ms-continuationtoken"])

$results

将显示:

Calling API
Token: 5
Calling API
Token: 10
Calling API

id             : 06cb494c-b535-4f16-9323-bd0f63c38163
name           : CMMI
url            : https://dev.azure.com/jessehouwing/_apis/projects/06cb494c-b535-4f16-9323-bd0f63c38163
state          : wellFormed
revision       : 414360096
visibility     : private
lastUpdateTime : 08/07/2019 20:19:09

id             : 88a7ca79-b5c8-41d2-99e7-a5578a1df424
name           : BattleJSip
description    : Battleship case in JS/TS
url            : https://dev.azure.com/jessehouwing/_apis/projects/88a7ca79-b5c8-41d2-99e7-a5578a1df424
state          : wellFormed
revision       : 414360059
visibility     : private
lastUpdateTime : 15/04/2018 13:43:44

... for 12 projects
/projects终结点的情况下,继续令牌采用要跳过的项目数的可预测值。其他API可能返回GUID或Base64编码的字符串或最后返回项的ID。不要假设令牌的内容,始终将其从标头复制并逐字放入下一个请求中。

例如

https://dev.azure.com/p/_apis/projects?api-version=6.0&$top=5

x-ms-continuation-token:5

{"count":5,"value":[{"id":"06cb494c-b535-4f16-9323-bd0f63c38163","name":"CMMI","url":"https://dev.azure.com/jessehouwing/_apis/projects/06cb494c-b535-4f16-9323-bd0f63c38163","state":"wellFormed","revision":414360096,"visibility":"private","lastUpdateTime":"2019-07-08T20:19:09.333Z"},{"id":"88a7ca79-b5c8-41d2-99e7-a5578a1df424","name":"BattleJSip","description":"Battleship case in JS/TS","url":"https://dev.azure.com/jessehouwing/_apis/projects/88a7ca79-b5c8-41d2-99e7-a5578a1df424","state":"wellFormed","revision":414360059,"visibility":"private","lastUpdateTime":"2018-04-15T13:43:44Z"},{"id":"f5ffbb7d-11bc-4e2f-93e0-e9ee8151b428","name":"CodeToCloud-Workshop","url":"https://dev.azure.com/jessehouwing/_apis/projects/f5ffbb7d-11bc-4e2f-93e0-e9ee8151b428","state":"wellFormed","revision":414360127,"visibility":"private","lastUpdateTime":"2020-10-01T18:21:03.913Z"},{"id":"6d4b20e3-4afc-4fb0-9dc9-f4b1d3ff150d","name":"Agile2017","url":"https://dev.azure.com/jessehouwing/_apis/projects/6d4b20e3-4afc-4fb0-9dc9-f4b1d3ff150d","state":"wellFormed","revision":414360110,"visibility":"private","lastUpdateTime":"2019-10-09T11:52:58.767Z"},{"id":"2031f1a3-c549-46f1-8c55-74390077a606","name":"jessehouwing.net","url":"https://dev.azure.com/jessehouwing/_apis/projects/2031f1a3-c549-46f1-8c55-74390077a606","state":"wellFormed","revision":414360067,"visibility":"private","lastUpdateTime":"2018-07-02T11:08:39.76Z"}]}

然后下一次调用

https://dev.azure.com/p/_apis/projects?api-version=6.0&$top=5&continuationToken=5

x-ms-continuation-token:10

{"count":5,"value":[{"id":"a88536a2-a889-45a3-a955-ddf1af8aeba1","name":"azure-devops-extensions","description":"This projects hosts the pipelines for all my Azure DevOps marketplace extensions.","url":"https://dev.azure.com/jessehouwing/_apis/projects/a88536a2-a889-45a3-a955-ddf1af8aeba1","state":"wellFormed","revision":414360082,"visibility":"public","lastUpdateTime":"2019-06-28T09:48:16.943Z"},{"id":"a1627c96-8627-41c7-9c29-498d523517e0","name":"Agile","url":"https://dev.azure.com/jessehouwing/_apis/projects/a1627c96-8627-41c7-9c29-498d523517e0","state":"wellFormed","revision":414360119,"visibility":"private","lastUpdateTime":"2019-12-02T12:11:23.863Z"},{"id":"a57ce751-1dcc-4089-b850-359624e92977","name":"Torpydo","description":"Battleship case in Python","url":"https://dev.azure.com/jessehouwing/_apis/projects/a57ce751-1dcc-4089-b850-359624e92977","state":"wellFormed","revision":414360038,"visibility":"private","lastUpdateTime":"2017-11-19T19:06:20.23Z"},{"id":"73711003-5adb-4e06-a7bb-f1d12b29db42","name":"Actual Scrum","url":"https://dev.azure.com/jessehouwing/_apis/projects/73711003-5adb-4e06-a7bb-f1d12b29db42","state":"wellFormed","revision":414360069,"visibility":"private","lastUpdateTime":"2019-04-09T19:26:38.877Z"},{"id":"9c643486-32f0-44f5-a49b-900b72f8219b","name":"Test","url":"https://dev.azure.com/jessehouwing/_apis/projects/9c643486-32f0-44f5-a49b-900b72f8219b","state":"wellFormed","revision":414360078,"visibility":"private","lastUpdateTime":"2019-05-27T09:58:21.247Z"}]}

将获取项目6到10,并为下一次调用发回continuation-token和值10。

直到x-ms-continuation-token头不再返回

https://dev.azure.com/p/_apis/projects?api-version=6.0&$top=5&continuationToken=10

{"count":2,"value":[{"id":"6484ebc3-af16-4af9-aa66-6b3398db7214","name":"demo","description":"This team is  meant to be used for all kinds of great demos","url":"https://dev.azure.com/jessehouwing/_apis/projects/6484ebc3-af16-4af9-aa66-6b3398db7214","state":"wellFormed","revision":414360040,"visibility":"private","lastUpdateTime":"2018-02-08T04:57:49.15Z"},{"id":"c3eb1420-aa31-4610-9bb1-30b9e3496967","name":"OhhShitGit","url":"https://dev.azure.com/jessehouwing/_apis/projects/c3eb1420-aa31-4610-9bb1-30b9e3496967","state":"wellFormed","revision":414360050,"visibility":"private","lastUpdateTime":"2018-03-13T10:41:22.567Z"}]}

博客:Accessing Azure DevOps APIs with large volumes of data

这篇关于PowerShell脚本中的Azure DevOps REST API TOP参数不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆