I am trying to get a target's capabilities under azure devops deployment group using Powershell REST API. However I am not sure which URL will work to fetch those capabilities. My Powershell script is working till fetching status of 'targets'. Please help if there is anything we can do to fetch capabilities.
Below is my script which is working till fetching target details:
$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"
}
}
}
To to fetch capabilities of the Deployment Group Targets, you can use the Rest API: Targets - Get. You need to add the parameter:
$expand=capabilitiesin the URL.Then you will get the capabilities of the Deployment Group Targets.
To use the Rest API URL in the PowerShell, you can use the following format:
Update:
When we add the
$expand=capabilitiesin the Rest API url, it will return the capabilities in the API Response.Here is an example: