Reputation: 1
# Define variables
$organization = "xxx"
$project = "xxxx"
$pat = "xxxxx"
$outputCsv = "agents_applications.csv"
# Generate a unique filename for the CSV file
$outputCsv = "agents_applications_$(Get-Date -Format 'yyyyMMdd_HHmmss').csv"
# Base64 encode the PAT
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(":$($pat)"))
# Fetch agent pools
$poolsUrl = "https://dev.azure.com/$organization/_apis/distributedtask/pools?api-version=7.1"
## "https://dev.azure.com/$organization/_apis/distributedtask/pools?api-version=7.1-preview.1"
$poolsResponse = Invoke-RestMethod -Uri $poolsUrl -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
# Initialize an array to hold the CSV data
$csvData = @()
# Fetch agents and application installation details for each pool
$poolsResponse.value | ForEach-Object {
$poolId = $_.id
$poolName = $_.name
# Fetch agents in the pool
$agentsUrl = "https://dev.azure.com/$organization/_apis/distributedtask/pools/$poolId/agents?api-version=7.1"
## "https://dev.azure.com/$organization/_apis/distributedtask/pools/$poolId/agents?api-version=7.1-preview.1"
$agentsResponse = Invoke-RestMethod -Uri $agentsUrl -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
# Fetch application installation details for each agent
$agentsResponse.value | ForEach-Object {
$agentId = $_.id
$agentName = $_.name
$agentDetailsUrl = "https://dev.azure.com/$organization/_apis/distributedtask/pools/$poolId/agents/$agentId?api-version=7.0"
$agentDetailsResponse = Invoke-RestMethod -Uri $agentDetailsUrl -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -debug
$agentDetailsResponse.systemCapabilities.GetEnumerator() | ForEach-Object {
$csvData += [PSCustomObject]@{
AgentPoolName = $poolName
AgentName = $agentName
Application = $_.Key
InstalledVersion = $_.Value
}
}
}
}
# Export the data to a CSV file
$csvData | Export-Csv -Path $outputCsv -NoTypeInformation
Write-Output "Data has been written to $outputCsv"
This script basically fails to fetch capabilities with error:
The controller for path '/_apis/distributedtask/pools/5/agents/-version=7.0' was not found or does not implement IController.
Any suggestions on how to fix this?
Is there any way to scan each pipeline and fetch what apps are being installed in agents or do we have direct script to get agents capabilities
try modifying powershell to azure CLI and using azuredevops module,nothing worked
Upvotes: 0
Views: 31
Reputation: 13944
You can use the Azure DevOps REST API "Agents - Get Agent" to get the capabilities (including System capabilities and User-defined capabilities) of a specified agent via adding the parameter 'includeCapabilities=true
' on the request URI.
GET https://dev.azure.com/{organization}/_apis/distributedtask/pools/{poolId}/agents/{agentId}?includeCapabilities=true&api-version=7.1
Similarly, you also can use the API "Agents - List" to get a list of agents and the capabilities of each listed agent in a specified pool.
GET https://dev.azure.com/{organization}/_apis/distributedtask/pools/{poolId}/agents?includeCapabilities=true&api-version=7.1
Upvotes: 0