Last active
April 12, 2021 15:50
-
-
Save akanieski/172f53fdea1c3fac4540e8f7b85d9b84 to your computer and use it in GitHub Desktop.
Azure DevOps - Find Old Agents
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param ( | |
[string]$Token = "", | |
[string]$OrgUrl = "", | |
[switch]$Quiet = $false, | |
[string]$MinVersion = "" | |
) | |
function Split-VersionString{ | |
param ( | |
[string]$versionString | |
) | |
$result = @() | |
foreach ($v in $versionString.Split('.')) { | |
$result += [int]::Parse($v); | |
} | |
return $result | |
} | |
if ("" -eq $minVersion) { | |
Write-Host "Please specify a -MinVersion `"1.2.3`"" -ForegroundColor Magenta | |
Exit 1 | |
} | |
if ("" -eq $Token) { | |
Write-Host "Please specify a -Token `"PAT TOKEN`"" -ForegroundColor Magenta | |
Exit 1 | |
} | |
if ("" -eq $OrgUrl) { | |
Write-Host "Please specify a -OrgUrl `"https://dev.azure.com/YourOrg`"" -ForegroundColor Magenta | |
Exit 1 | |
} | |
if ($true -eq $Quiet) { | |
$VerbosePreference="SilentlyContinue" | |
} | |
## auth into az devops, can use Windows Auth or PAT | |
echo $token | az devops login | |
$pools = az pipelines pool list --org $OrgUrl --only-show-errors | ConvertFrom-Json | |
$min = Split-VersionString($minVersion) | |
Write-Verbose "Parsed input minimum version as $($min[0])|$($min[1])|$($min[2])" | |
Write-Host "Scanning for agents older than $($minVersion)" | |
$oldAgents = @() | |
foreach ($pool in $pools) { | |
if (!$pool.isHosted) { | |
Write-Verbose "Checking [$($pool.name)] for old agents." | |
$agents = az pipelines agent list --pool-id $pool.id --org $OrgUrl --only-show-errors | ConvertFrom-Json | |
if ($agents.Count -gt 0) { | |
Write-Verbose "Scanning [$($agents.Count)] agents in [$($pool.name)].." | |
} | |
foreach ($agent in $agents) { | |
$version = Split-VersionString($agent.version) | |
if (-not ($version[0] -gt $min[0] -or | |
($version[0] -gt $min[0] -and $version[1] -gt $min[1]) -or | |
($version[0] -gt $min[0] -and $version[1] -gt $min[1] -and $version[2] -gt $min[2]))) { | |
$oldAgents += [psCustomObject]@{ | |
AgentId = $agent.id | |
AgentName = $agent.name | |
AgentStatus = $agent.status | |
AgentVersion = $agent.version | |
AgentOS = $agent.osDescription | |
AgentCreationDate = $agent.createdOn | |
AgentEnabled = $agent.enabled | |
AgentLastCompletedRequestDate = $agent.lastCompletedRequest | |
AgentProvisioningState = $agent.provisioningState | |
PoolId = $pool.id | |
PoolName = $pool.name | |
PoolCreationDate = $pool.createdOn | |
PoolOwnerName = $pool.owner.displayName | |
PoolOwnerUniqueName = $pool.owner.uniqueName | |
PoolCreatorName = $pool.createdBy.displayName | |
PoolCreatorUniqueName = $pool.createdBy.uniqueName | |
} | |
Write-Verbose "Found old agent [$($agent.name)] with version [$($agent.version)]" | |
} else { | |
Write-Verbose "Skipping agent [$($agent.name)] with version [$($agent.version)]" | |
} | |
} | |
} else { | |
Write-Verbose "Skipping hosted pool [$($pool.name)]." | |
} | |
} | |
Write-Host "Found $($oldAgents.Count) old agents!" | |
return $oldAgents |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment