Created
March 15, 2024 19:44
-
-
Save caseywatson/81f46280cdc1c7ae171e7d62559a4e82 to your computer and use it in GitHub Desktop.
Check Azure resource quotas
This file contains hidden or 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( | |
[switch]$Help, | |
[int]$WarningLevelPct = 70, | |
$SubscriptionId, | |
$ResourceProvider, | |
$QuotaApiToken | |
) | |
if ($Debug.IsPresent) { | |
$DebugPreference = "Continue" | |
} | |
else { | |
$DebugPreference = "SilentlyContinue" | |
} | |
Clear-Host | |
function Variables { | |
$Global:QuotaApiToken = $QuotaApiToken | |
} | |
function CheckParams { | |
Write-Host "Checking parameters..." -ForegroundColor Cyan | |
$success = $true | |
if ([string]::IsNullOrEmpty($SubscriptionId)) { | |
Write-Host "-SubscriptionId is required." -ForegroundColor Red | |
$success = $false | |
} | |
if ([string]::IsNullOrEmpty($ResourceProvider)) { | |
Write-Host "-ResourceProvider is required." -ForegroundColor Red | |
$success = $false | |
} | |
return $success | |
} | |
function Subscription { | |
Get-AzSubscription -SubscriptionId "$SubscriptionId" | Select-AzSubscription | Out-Null | |
} | |
function Help { | |
Write-Host | |
Write-Host "This script checks the capacity of an Azure resource provider (ResourceProvider) in a subscription (SubscriptionId)." | |
Write-Host | |
Write-Host "This script requires:" | |
Write-Host | |
Write-Host " - The Az.Accounts module." | |
Write-Host " - The Microsoft.Capacity resource provider registered in the subscription." | |
Write-Host | |
Write-Host "Parameters" | |
Write-Host | |
Write-Host "-SubscriptionId <string> Required; the Azure subscription ID." | |
Write-Host "-ResourceProvider <string> Required; the Azure resource provider name (e.g., Microsoft.Compute)." | |
Write-Host "-QuotaApiToken <string> Optional; the Azure Quota API token. If not provided, user will be prompted to authenticate." | |
Write-Host | |
Write-Host "Examples" | |
Write-Host | |
Write-Host "Check Microsoft.Compute capacity in subscription 00000000-0000-0000-0000-000000000000 using interactive user auth:" | |
Write-Host | |
Write-Host " .\check-capacity.ps1 ` " | |
Write-Host " -SubscriptionId '00000000-0000-0000-0000-000000000000' ` " | |
Write-Host " -ResourceProvider 'Microsoft.Compute' " | |
Write-Host | |
Write-Host "Check Microsoft.Compute capacity in subscription 00000000-0000-0000-0000-000000000000 using QuotaApiToken auth:" | |
Write-Host | |
Write-Host " .\check-capacity.ps1 ` " | |
Write-Host " -SubscriptionId '00000000-0000-0000-0000-000000000000' ` " | |
Write-Host " -ResourceProvider 'Microsoft.Compute' ` " | |
Write-Host " -QuotaApiToken '00000000-0000-0000-0000-000000000000' " | |
Write-Host | |
} | |
function CheckRequirements { | |
Write-Host "Checking requirements..." -ForegroundColor Cyan | |
$AzModules = Get-Module -Name Az.Accounts -ListAvailable -ErrorAction SilentlyContinue | |
if ($null -eq $AzModules) { | |
Write-Host "Az.Accounts module not installed; installing now..." -ForegroundColor Yellow | |
Install-Module -Name Az.Accounts -SkipPublisherCheck -InformationAction SilentlyContinue | |
} | |
$capacityRp = $(Get-AzResourceProvider -ProviderNamespace "Microsoft.Capacity")[0] | |
if ($capacityRp.RegistrationState -ne "Registered") { | |
Write-Host "Microsoft.Capacity resource provider not registered; registering now..." -ForegroundColor Yellow | |
Register-AzResourceProvider -ProviderNamespace "Microsoft.Capacity" -InformationAction SilentlyContinue | |
for ($i = 0; $i -lt 10; $i++) { | |
if ($(Get-AzResourceProvider -ProviderNamespace "Microsoft.Capacity")[0].RegistrationState -eq "Registered") { | |
return $true | |
} | |
else { | |
$sleepFor = [Math]::Min(10, [Math]::Pow(2, $i)) | |
Write-Host "Waiting for Microsoft.Capacity resource provider to register... ($sleepFor seconds)" -ForegroundColor Yellow | |
Start-Sleep -Seconds $sleepFor | |
} | |
} | |
} | |
else { | |
return $true | |
} | |
return $false | |
} | |
function Authenticate { | |
Write-Host "Authenticating..." -ForegroundColor Cyan | |
Connect-AzAccount -WarningAction SilentlyContinue | Out-Null | |
$Global:QuotaApiToken = (Get-AzAccessToken).Token | |
} | |
function CheckCapacity { | |
$checkCapResponse = Invoke-RestMethod ` | |
-Method GET ` | |
-ContentType "application/json" ` | |
-Uri "https://management.azure.com/subscriptions/$SubscriptionId/providers/Microsoft.Capacity/resourceProviders/$ResourceProvider/locations/eastus/serviceLimits?api-version=2020-10-25" ` | |
-Headers @{ "Authorization" = "Bearer $($Global:QuotaApiToken)" } | |
$rows = @() | |
for ($i = 0; $i -lt $checkCapResponse.value.Count; $i++) { | |
$cap = $checkCapResponse.value[$i] | |
$capName = $cap.properties.name.value | |
$capLimit = $cap.properties.limit | |
$capCurrent = $cap.properties.currentValue | |
if ($capLimit -gt 0) { | |
$capPct = [math]::Round($capCurrent / $capLimit, 2) | |
$capPct = [math]::Round($capPct * 100, 0) | |
if ($capPct -ge $WarningLevelPct) { | |
$rows += [PSCustomObject]@{ | |
Name = $capName | |
Limit = $capLimit | |
Current = $capCurrent | |
Pct = "⚠️ $capPct%" | |
} | |
} | |
else { | |
$rows += [PSCustomObject]@{ | |
Name = $capName | |
Limit = $capLimit | |
Current = $capCurrent | |
Pct = if ($capPct -gt 0) { "✅ $capPct%" } else { " 0%" } | |
} | |
} | |
} | |
else { | |
$rows += [PSCustomObject]@{ | |
Name = $capName | |
Limit = $capLimit | |
Current = $capCurrent | |
Pct = " 0%" | |
} | |
} | |
} | |
if ($rows.Count -eq 0) { | |
Write-Host "No capacity limits found." -ForegroundColor Yellow | |
} | |
else { | |
$rows | Format-Table -AutoSize | Out-Host | |
} | |
} | |
if ($Help) { | |
Help | |
} | |
else { | |
if (CheckParams) { | |
Authenticate | |
Subscription | |
if (CheckRequirements) { | |
CheckCapacity | |
} | |
} | |
else { | |
Help | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment