-
Notifications
You must be signed in to change notification settings - Fork 2
/
azureResourceGraph_API.ps1
74 lines (63 loc) · 3.01 KB
/
azureResourceGraph_API.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#using the cmdlet here as this is handling nextLink
$relevantSubscriptions = (Get-AzSubscription -ErrorAction Stop).where( { $_.State -eq "Enabled" -and $_.SubscriptionPolicies.QuotaId -notlike "AAD*" }).Id
$relevantSubscriptionsCount = $relevantSubscriptions.Count
<#todo: nextLink handling
https://docs.microsoft.com/en-us/rest/api/resources/subscriptions/list
$path = "/subscriptions?api-version=2020-01-01"
$method = "GET"
$getSubscriptions = Invoke-AzRestMethod -Method $method -Path $path
$relevantSubscriptions = ($getSubscriptions.Content | ConvertFrom-Json).value.where( { $_.State -eq "Enabled" -and $_.SubscriptionPolicies.QuotaId -notlike "AAD*" } )
$relevantSubscriptionsCount = $relevantSubscriptions.count
#>
Write-Host "Running ARG query against $($relevantSubscriptionsCount) Subscriptions"
$data = [System.Collections.ArrayList]@()
$query = "resources | project id, name"
#Batching: https://docs.microsoft.com/en-us/azure/governance/resource-graph/troubleshoot/general#toomanysubscription
$counterBatch = [PSCustomObject] @{ Value = 0 }
$batchSize = 3 #max=1000
Write-Host "Subscriptions Batch size: $batchSize"
$subscriptionsBatch = $relevantSubscriptions | Group-Object -Property { [math]::Floor($counterBatch.Value++ / $batchSize) }
$batchCnt = 0
foreach ($batch in $subscriptionsBatch) {
$startBatch = get-date
$batchCnt++
Write-Host " processing Batch #$batchCnt/$(($subscriptionsBatch | Measure-Object).Count) ($(($batch.Group).Count) Subscriptions)" -ForegroundColor Yellow
$subscriptions = '"{0}"' -f ($batch.Group -join '","')
$path = "/providers/Microsoft.ResourceGraph/resources?api-version=2021-03-01"
$method = "POST"
$payload = @"
{
"query": "$($query)",
"subscriptions": [$($subscriptions)]
}
"@
$iteration = 0
do {
$iteration++
Write-Host " Batch #$($batchCnt); POST #$($iteration)"
$invoke = Invoke-AzRestMethod -Method $method -Payload $payload -Path $path
$result = $invoke.Content | ConvertFrom-Json
Write-Host " Batch #$($batchCnt); POST #$($iteration) Returned records: $($result.'count')" -ForegroundColor Green
$null = $data.AddRange($result.data)
Write-Host " Batch #$($batchCnt); POST #$($iteration) Array items (total): $($data.Count)" -ForegroundColor Blue
if ($result.'$skipToken') {
Write-Host " Batch #$($batchCnt); POST #$($iteration) SkipToken present ($($result.'$skipToken'))" -ForegroundColor Cyan
$payload = @"
{
query: "$($query)",
subscriptions: [$($subscriptions)],
options: {
`$skipToken: "$($result.'$skipToken')"
}
}
"@
}
else {
Write-Host " Batch #$($batchCnt); POST #$($iteration) SkipToken NOT present"
}
}
until(-not $result.'$skipToken')
$endBatch = get-date
Write-Host " Batch #$batchCnt processing duration: $((NEW-TIMESPAN -Start $startBatch -End $endBatch).TotalMinutes) minutes ($((NEW-TIMESPAN -Start $startBatch -End $endBatch).TotalSeconds) seconds)"
}
Write-Host "Resources:" $data.count