I am new to devops and azure, Delete all offline agents in my agent pools
CodePudding user response:
I have developed this little powershell script that will get list of all agents in specified agent pool and will delete only offline agents from pool. Replace values according to your need in {} brackets below snipet.
Code Snipet:
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Basic {base 64 encode PAT}XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
$agents = Invoke-RestMethod 'https://dev.azure.com/{your_organization_name}/_apis/distributedtask/pools/{pool_id}/agents' -Method 'GET' -Headers $headers
$agents.value |
Where-Object { $_.status -eq 'offline' } |
ForEach-Object {
Invoke-RestMethod https://dev.azure.com/{your_organization_name}}/_apis/distributedtask/pools/{pool_id}/agents/$($_.id)?api-version=6.0 -Method 'Delete' -Headers $headers
}
You can always get agent pool id using postman of any api tool by selecting auth type Basic Auth and PAT as your password. I am more comfortable using postman :)