Checking resource limits, quotas, and constraints using PowerShell

Jason Crawford 0 Reputation points
2023-01-24T15:23:04.43+00:00

I am looking for a solution to check for resource limit quotas that have been reached in multiple subscriptions using Lighthouse. Has anyone found a way to accomplish this using PowerShell, Graph, or other methods? Essentially, I am looking for a way to loop through each subscription and output any limits that have been reached. I have added some code on the kind of thing I am trying do. Please note, the provided code is for demonstration purposes only and may contain incorrect commands

Get-AzSubscription | Where-Object {$_.QuotaInfo.ResourceLimits.Count -gt 0} | ForEach-Object {
    Write-Host "Subscription: $( $_.Name )"
    $_.QuotaInfo.ResourceLimits | ForEach-Object {
        Write-Host "    Resource: $( $_.ResourceName )"
        Write-Host "    Limit: $( $_.Limit )"
        Write-Host "    Current Usage: $( $_.CurrentUsage )"
    }
}
# Get all subscriptions
$subscriptions = Get-AzSubscription

# Loop through each subscription
foreach ($subscription in $subscriptions) {
    # Select the subscription
    Select-AzSubscription -SubscriptionId $subscription.Id

    # Get resource limits for the subscription
    $resourceLimits = Get-AzResourceProvider -ListAvailable | Where-Object {$_.ResourceTypes.Quota -ne $null}

    # Loop through each resource limit
    foreach ($resourceLimit in $resourceLimits) {
        # Check if the limit has been reached
        if ($resourceLimit.CurrentValue -ge $resourceLimit.Limit) {
            # Output the subscription name and resource limit
            Write-Output "Subscription: $($subscription.Name)"
            Write-Output "Resource: $($resourceLimit.ResourceType)"
            Write-Output "Limit: $($resourceLimit.Limit)"
            Write-Output "Current Usage: $($resourceLimit.CurrentValue)"
        }
    }
}

Azure Lighthouse
Azure Lighthouse
An Azure service that provides secure managed services and access control for partners and customers.
71 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
11,264 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,274 questions
{count} votes