Created
April 5, 2022 00:31
-
-
Save andyrobbins/be12067f17d5511457229d038f212bc6 to your computer and use it in GitHub Desktop.
List all ACR tasks across all subscriptions
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
Function Get-ARMAPIToken { | |
<# | |
.DESCRIPTION | |
Requests a token from STS with the MS Graph specified as the resource/intended audience | |
#> | |
[cmdletbinding()] | |
param( | |
[Parameter(Mandatory = $True)] | |
[string] | |
$ClientID, | |
[Parameter(Mandatory = $True)] | |
[string] | |
$ClientSecret, | |
[Parameter(Mandatory = $True)] | |
[string] | |
$TenantName | |
) | |
$Body = @{ | |
Grant_Type = "client_credentials" | |
Scope = "https://management.azure.com/.default" | |
client_Id = $ClientID | |
Client_Secret = $ClientSecret | |
} | |
$Token = Invoke-RestMethod ` | |
-URI "https://login.microsoftonline.com/$TenantName/oauth2/v2.0/token" ` | |
-Method POST ` | |
-Body $Body | |
$Token | |
} | |
# Get a token for AzureRM API token: | |
$Token = Get-ARMAPIToken ` | |
-ClientID "<your client ID>" ` | |
-ClientSecret "<your client secret>" ` | |
-TenantName "<your Azure tenant name>" | |
# Get list of subscriptions we can read: | |
$Subscriptions = $null | |
$URI = 'https://management.azure.com/subscriptions?api-version=2020-01-01' | |
$Subscriptions = Invoke-RestMethod ` | |
-Headers @{Authorization = "Bearer $($Token.access_token)"} ` | |
-URI $URI ` | |
-UseBasicParsing ` | |
-Method "GET" ` | |
-ContentType "application/json" | |
# Get list of ACRs under each subscription: | |
$ACRs = $null | |
ForEach ($Sub In $Subscriptions.value) { | |
$URI = "https://management.azure.com/subscriptions/$($Sub.subscriptionId)/providers/Microsoft.ContainerRegistry/registries?api-version=2019-05-01" | |
$Req = Invoke-RestMethod ` | |
-Headers @{Authorization = "Bearer $($Token.access_token)"} ` | |
-URI $URI ` | |
-UseBasicParsing ` | |
-Method "GET" ` | |
-ContentType "application/json" | |
If ($Req.value) { | |
$ACRs += $Req.value | |
} | |
} | |
# Get list of tasks under each container registry: | |
$Tasks = $null | |
ForEach ($Registry In $ACRs) { | |
$URI = "https://management.azure.com/$($Registry.id)/tasks?api-version=2019-04-01" | |
$Req = Invoke-RestMethod ` | |
-Headers @{Authorization = "Bearer $($Token.access_token)"} ` | |
-URI $URI ` | |
-UseBasicParsing ` | |
-Method "GET" ` | |
-ContentType "application/json" | |
If ($Req.value) { | |
$Tasks += $Req.value | |
} | |
} | |
ForEach ($Task In $Tasks) { | |
Write-Host "Task ID:" $Task.id | |
Write-Host "Task Git repo:" $Task.properties.step.contextPath | |
Write-Host "Task YAML:" $Task.properties.step.taskFilePath | |
Write-Host "" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment