-
Notifications
You must be signed in to change notification settings - Fork 2
/
RemoveAgents.ps1
45 lines (31 loc) · 1.66 KB
/
RemoveAgents.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
[CmdletBinding()]
Param(
[string]$VSTSToken = $env:VSTSToken,
[string]$VSTSUrl = $env:VSTSUrl,
$agentPoolPattern = "AgentVM"
)
$ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest
$apiVersion = "3.0-preview.1"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f "", $VSTSToken)))
$uri = "${VSTSUrl}/_apis/distributedtask/pools?api-version=${apiVersion}"
"Calling $uri"
$allPoolsResult = Invoke-RestMethod -Uri $uri -Method Get -ContentType "application/json" -Headers @{Authorization = ("Basic {0}" -f $base64AuthInfo)}
$allPoolsResult
foreach ($poolRec in $allPoolsResult.value) {
"Processing Agent Pool $poolRec.name"
# Get agents of an agent pool (Request method: Get):
$uri = "${VSTSUrl}/_apis/distributedtask/pools/$( $poolRec.id )/agents?api-version=${apiVersion}"
$thisPoolResult = Invoke-RestMethod -Uri $uri -Method Get -ContentType "application/json" -Headers @{Authorization = ("Basic {0}" -f $base64AuthInfo)}
foreach ($agentRec in $thisPoolResult.value) {
"Processing Agent $agentRec.name"
if ($agentRec.name -match $agentPoolPattern) {
if ($agentRec.status -eq "offline") {
Write-Host "Deleting Agent '$( $agentRec.name )'" -ForegroundColor Red
#Delete an agent from an agent pool (Request method: Delete):
$uri = "${VSTSUrl}/_apis/distributedtask/pools/$( $poolRec.id )/agents/$( $agentRec.id )?api-version=${apiVersion}"
Invoke-RestMethod -Uri $uri -Method Delete -ContentType "application/json" -Headers @{Authorization = ("Basic {0}" -f $base64AuthInfo)}
}
}
}
}