【问题标题】:Get list of all installed Build Agents in TFS 2018获取 TFS 2018 中所有已安装构建代理的列表
【发布时间】:2019-01-02 05:40:05
【问题描述】:

是否可以获得已安装的所有可用构建代理的列表?

我正在使用 TFS 2018 并希望获取包含所有构建代理的 excel,这些构建代理沿其托管的机器名称安装。如果可以,怎么做?

【问题讨论】:

  • 您是否尝试过在https://{your_server}/DefaultCollection/_admin/_AgentPool 上对您的代理池执行任何操作,如Build and Release Agents MS Doc 中所述?
  • 我不这么认为。我只能在每个构建代理池前面看到代理,这很好,但我正在寻找所有代理池中所有代理的列表。

标签: powershell tfs msbuild continuous-integration azure-devops


【解决方案1】:

您可以创建 Powershell 脚本(使用 Invoke-RestMethod)来使用 REST Api 并获取代理。

例如:

http://TFS-SERVER:8080/tfs/_apis/distributedtask/pools/1/agents?includeCapabilities=false&includeAssignedRequest=true

在上面的 api 中,您将获取池 1 中的所有代理,您可以遍历所有池 id 并获取所有代理,然后将数据导出到 excel。

有关 Powershell here 的 Invoke-RestMethod 的更多信息。

【讨论】:

    【解决方案2】:

    您可以使用下面的 Powershell 脚本来获取所有代理池中的代理列表并导出到 excel(可以在 excel 中打开的*.csv 文件):

    Param(
       [string]$collectionurl = "http://server:8080/tfs",
       [string]$user = "username",
       [string]$token = "password",
       [string]$path = "D:\temp" # Export the agent list (*.csv file) to this path
    
    )
    
    $filename = (Get-Date).ToString("yyyyMMdd-HHmmss") + "-" + "AgentList.csv"
    
    # Base64-encodes the Personal Access Token (PAT) appropriately
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
    
    #Get pools 
    $poolsUrl = "$collectionurl/_apis/distributedtask/pools"            
    $poolresponse = Invoke-RestMethod -Uri $poolsUrl -Method Get -UseDefaultCredential -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
    $pools = $poolresponse.value
    
    foreach ($pool in $pools )
    {
    #Get agent list from pool
    $baseUrl = "$collectionurl/_apis/distributedtask/pools/$($pool.id)/agents?includeCapabilities=true&includeAssignedRequest=true"         
    $agentresponse = Invoke-RestMethod -Uri $baseUrl -Method Get -UseDefaultCredential -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
    $agents = $agentresponse.value
    
    $agentlist = @()
    
       foreach ($agent in $agents)
       {
    
        $customObject = new-object PSObject -property @{
              "PoolName" = $pool.name
              "Agent.Name" = $agent.systemCapabilities.'Agent.Name'
              "Agent.Version" = $agent.systemCapabilities.'Agent.Version'
              "Agent.ComputerName" = $agent.systemCapabilities.'Agent.ComputerName'
            } 
    
        $agentlist += $customObject
       }
    
       $agentlist | Select `
                    PoolName,
                    Agent.Name,
                    Agent.Version, 
                    Agent.ComputerName |export-csv -Path $path\$filename -NoTypeInformation -Append
    
    }
    

    【讨论】:

    • 是的,它成功了。谢谢!!
    • @DevOps 很高兴知道它对您有用,请Accept it as an Answer,这可能对阅读此主题的其他社区成员有所帮助。
    • 我点击了你发送的链接。还有其他 Wat 可以接受答案吗?
    • @DevOps 要将答案标记为已接受,请单击答案旁边的复选标记(在答案的左上角)从空心到绿色 b> meta.stackexchange.com/a/5235
    猜你喜欢
    • 2018-08-15
    • 1970-01-01
    • 2021-10-30
    • 2019-01-02
    • 2011-11-05
    • 2018-06-19
    • 2017-04-07
    • 2018-10-21
    • 2018-06-07
    相关资源
    最近更新 更多