簡體   English   中英

Azure DevOps REST API PowerShell 腳本中的頂部參數不起作用

[英]Azure DevOps REST API top parameter in PowerShell script is not working

我有以下腳本從 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 中添加 top 參數,它可以在我上面的 PowerShell 腳本中運行嗎?

請務必轉義查詢字符串中的$ ,否則 PowerShell 將嘗試注入名為top的任何變量的值:

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

$top=有時很棘手。 它可能會或可能不會為您提供所請求的項目數量,這似乎取決於后端的繁忙程度。 但它確實 promise 你這樣做:所有 REST API 都會在有效負載旁邊返回一個x-ms-continuation-token header,你可以使用它來獲取下一批項目:

在此處輸入圖像描述

您可以通過請求完全相同的 REST 查詢並將&continuationtoken=${ms-continuation-token}查詢參數添加到調用來獲取下一批項目。 重復此操作,直到服務器停止發送x-ms-continuation-token header,這表示您已獲得所有想要的值。

您可以通過傳入第二個變量來獲取標頭:

$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。 對token的內容不作任何假設,總是從header中復制過來,逐字放入下一個請求中。

例如

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 並發回一個值為 10 的continuation-token用於下一次調用。

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

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"}]}

博客: 訪問具有大量數據的 Azure DevOps API

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM