【问题标题】:How to get 'Deployment group's target's capabilities in Azure Devops using Powershell rest如何使用 Powershell rest 在 Azure Devops 中获取“部署组”目标的功能
【发布时间】:2022-09-28 20:38:43
【问题描述】:

我想得到一个目标的能力在下面azure devops 部署组使用 Powershell REST API。但是我不确定哪个 URL 可以获取这些功能。我的 Powershell 脚本一直在工作,直到获取 \'targets\' 的状态。如果我们可以做任何事情来获取功能,请提供帮助。

下面是我的脚本,它一直工作到获取目标详细信息:

$projects = \"testing\"
$projectlist = $projects.split(\';\')
$PAT = \"#######################################33\"
$Header =  @{Authorization = \'Basic \' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(\":$($PAT)\")) }      
foreach($projectName in $projectlist){
    write-host \"=================================================\"
    $baseURL = \"https://dev.azure.com/abc/$($projectName)/_apis/distributedtask/deploymentgroups\" 
    $deploymentgroup=Invoke-RestMethod -Uri \"https://dev.azure.com/abc/$($projectName)/_apis/distributedtask/deploymentgroups?api-version=6.0-preview.1\" -Method get -Headers $Header
    $deploymentgroupsname=$deploymentgroup.value.name
    foreach($deploymentgroupname in $deploymentgroupsname){
    $deploymentGroupURL = \"$($baseURL)?name=$($deploymentgroupname)&api-version=6.0\"
    try{
      $deploymentgroup=Invoke-RestMethod -Uri \"$deploymentGroupURL\" -Method get -Headers $Header
    }catch{
      write-host \"URL is not accessible - $deploymentGroupURL\"
    }
    $deploymentGroupResponse=$deploymentgroup.value
    $deploymentGroupid=$deploymentGroupResponse.id
    try{
      $targets=Invoke-RestMethod -Uri \"https://dev.azure.com/abc/$($projectName)/_apis/distributedtask/deploymentgroups/$($deploymentGroupid)/targets?api-version=6.0-preview.1\" -Method get -Headers $Header
    }catch{
      write-host \"URL is not accessible - $deploymentGroupURL\"
    }
    if($null -ne $deploymentGroupId){
        $targets.value.agent|select name, status|%{
        $hostname=$_.name
        $Status=$_.status
        if($status -eq \"offline\"){
            $targetURL = \"$($baseURL)/$deploymentGroupId/targets?name=$($hostName)&api-version=6.0-preview.1\"
            try{ 
                $target = (Invoke-RestMethod -Uri $targetURL -Method get -Headers $Header).value 
                $targetId = $target.id ;
                if($null -ne $targetId){
                   $url = \"$($baseURL)/$deploymentGroupId/targets/$($targetId)?api-version=6.0\"
                   try{
                    write-host \"Projectname is : $projectName\"
                    write-host \"deploymentGroupname is : $deploymentgroupname\"  
                    write-host \"Server $hostname is not pingble\"
                    }
                   catch{
                      write-host \"TARGET DELETE ERROR: $hostName\";Write-Error $_.Exception.Message
                    }
                }
                else{ 
                    write-host \"Target $hostName NOT Found in DeploymentGroup $environment.\"
                }
             }catch {
                write-host \"TARGET LIST ERROR\";Write-Error $_.Exception.Message
              }
         }         
        }
    }else{
           write-host \"DeploymentGroup $deploymentgroupname NOT FOUND in $projectName\"
        }    
 }
} 

    标签: powershell azure-devops-rest-api azure-devops-deploymentgroups


    【解决方案1】:

    要获取部署组目标的功能,您可以使用 Rest API:Targets - Get。您需要在 URL 中添加参数:$expand=capabilities

    GET https://dev.azure.com/{organization}/{project}/_apis/distributedtask/deploymentgroups/{deploymentGroupId}/targets/{targetId}?$expand=capabilities&api-version=6.0-preview.1
    

    然后您将获得部署组目标的功能。

    要在 PowerShell 中使用 Rest API URL,可以使用以下格式:

    $url="https://dev.azure.com/{organization}/{project}/_apis/distributedtask/deploymentgroups/{deploymentGroupId}/targets/{targetId}?`$expand=capabilities&api-version=6.0-preview.1"
    

    更新:

    当我们在 Rest API url 中添加 $expand=capabilities 时,它将在 API Response 中返回功能。

    这是一个例子:

    【讨论】:

    • 嗨@kevin - 您提供输出共享的 URL,但不是这些功能下的详细信息,例如 - USERDOMAIN、Agent.version、Agent.os 等
    • 是的。我能理解你的要求。当我们将参数: $expand=capabilities 添加到 url 时,它将返回功能。参考我的更新。
    • 另一方面,当您在 PowerShell 中使用 url 时,您需要在 $expand 之前添加字符:`。例如:dev.azure.com{organization}/{project}/_apis/distributedtask/deploymentgroups/{deploymentGroupId}/targets/{targetId}?`$expand=capabilities&api-version=6.0-preview.1
    • 很高兴知道 API 可以为您工作!如果答案可以解决问题,您可以考虑接受它。这将有助于其他有相同问题的成员
    【解决方案2】:

    脚本是否与扩展功能一起使用?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-11
      相关资源
      最近更新 更多