首页 > 解决方案 > 使用 Build Agent Name 获取 Build Agent ID

问题描述

在触发回归之前,我正在使用 Powershell 脚本自动化构建代理的过程,该脚本将在每天的设定时间运行。我无法使用构建代理名称获取构建代理 ID,其中构建代理名称将由用户提供并在排队时设置。

尝试搜索这方面的所有内容,但还没有发现任何有用的东西(在我的上下文中)。我围绕这部分尝试了一些代码。主要关心的是我能够得到计数,但现在是值。检查输出,我得到以下是我尝试过的代码。

// Getting the list of Agent ID's from the Environmental Variables, splitting & saving in an array.
$agentID = $env:agentIDs -split ","
$URI = "https://URL.com/_apis/distributedtask/pools/xx/agents"
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type","application/json")
$headers.Add("Authorization","Bearer $env:SYSTEM_ACCESSTOKEN")
foreach ($element in $agentID)
{
  $poolresponse = Invoke-RestMethod -Uri $URI -Method GET -Headers $Headers
  echo $poolresponse
  // Count is giving the count correct.
  echo "count : $($poolresponse.count)"
  // However struggling with getting the value.
  echo "value : $($poolresponse.value)"
}

实际输出:

count value                                                                                                            

----- -----                                                                                                            

   10 {@{systemCapabilities=; _links=; maxParallelism=1; createdOn=; authorization=; id=133;...}]}
count : 10
value :

任何指导将不胜感激。

标签: powershelldevops

解决方案


由于它在值中返回一个数组,因此不显示。我将它用作:$poolresponse.value[$i]在 for 循环中它工作得非常好。

虽然感谢@LotPings 的帮助。


推荐阅读