Get Azure subscription quota limits using PowerShell

Today I want to show you function to gather Azure subscription quota limits.

Recently for one of the customer I had a problem with quota limit for static IP addresses.
Standard limit was reached quite quickly and I had to create support request in Azure to increase this limit.
To be more proactive in the future I’ve created script which gather all Azure subscription quota limits and show usage in percents.

As a input function takes following parameters

  • Location – it applies for VM and networks resources. All Azure locations are available under command Get-AzureRmLocation.
  • PercentageUsageLimit – this integer parameter define user custom limit of quota. In the result all resources over this limit will be marked in red, below in green.
  • CheckStorageUsage – this boolean parameter define if function should check storage usage.
  • CheckVMResourcesUsage – this boolean parameter define if function should check vm resources usage.
  • CheckNetworkResourcesUsage – this boolean parameter define if function should check network resources usage.
Script:
function Get-AzureQuotaLimit {
    [cmdletbinding()]
    Param (
        [Parameter(Position = 0, Mandatory = $true, HelpMessage = "Azure location", ValueFromPipeline = $false)] 
        $Location,
        [Parameter(Position = 1, Mandatory = $true, HelpMessage = "Percentage usage limit", ValueFromPipeline = $false)]
        [ValidateRange(1, 99)] 
        [int]$PercentageUsageLimit,
        [Parameter(Position = 2, Mandatory = $true, HelpMessage = "Check Storage usage", ValueFromPipeline = $false)]
        [ValidateNotNullOrEmpty()]
        [ValidateSet($false, $true)]
        $CheckStorageUsage,
        [Parameter(Position = 3, Mandatory = $true, HelpMessage = "Check VM resources usage", ValueFromPipeline = $false)]
        [ValidateNotNullOrEmpty()]
        [ValidateSet($false, $true)]
        $CheckVMResourcesUsage,
        [Parameter(Position = 4, Mandatory = $true, HelpMessage = "Check network resources usage", ValueFromPipeline = $false)]
        [ValidateNotNullOrEmpty()]
        [ValidateSet($false, $true)]
        $CheckNetworkResourcesUsage

    )
    begin {
        If (!(Get-AzureRmContext)) {
            Write-Host "Please login to your Azure account"
            Login-AzureRmAccount
        }

        $AllAvailableLocations = (Get-AzureRmLocation).Location

        if ($AllAvailableLocations -notcontains $Location) {
            Write-Error "Provided location name is not proper name of the available locations. Use one of the location from below list $AllAvailableLocations"
            break
        }
    }
    process {
        if ($CheckStorageUsage -eq $true) {

            $StorageUsageArray = @()
            $StorageUsage = Get-AzureRmStorageUsage
            $StorageUsageLimit = $StorageUsage.Limit
            $StorageUsageCurrentStatus = $StorageUsage.CurrentValue
            $StoragePercentageUsage = [Math]::Round(($StorageUsageCurrentStatus / $StorageUsageLimit) * 100)
            $Properties = @{
                "Resource Name"      = $StorageUsage.Name
                "Percentage of usage" = $StoragePercentageUsage
            }
   
            $StorageUsageObject = New-Object PSObject -Property $Properties
            $StorageUsageArray += $StorageUsageObject
        }

        if ($CheckVMResourcesUsage -eq $true) {
            $VMUsageOverQuota = @()
            $VMUsageBelowQuota = @()
            $VMUsage = Get-AzureRmVMUsage -Location $Location
            foreach ($VMResource in $VMUsage) {
                $ResourceName = $VMResource.Name.LocalizedValue
                $ResourceUsageLimit = $VMResource.Limit
                $ResouceUsageCurrentStatus = $VMResource.CurrentValue
                if ($ResourceUsageLimit -ne 0) {
                    $ResourcePercentageUsage = [Math]::Round(($ResouceUsageCurrentStatus / $ResourceUsageLimit) * 100)
                    if ($ResourcePercentageUsage -gt $PercentageUsageLimit) {
                        $Properties = @{
                            "Resource Name"      = $ResourceName
                            "Percentage of usage" = $ResourcePercentageUsage
                        }
               
                        $ResourceUsageOverQuota = New-Object PSObject -Property $Properties
                        $VMUsageOverQuota += $ResourceUsageOverQuota
                    }
                    else {
                        $Properties = @{
                            "Resource Name"      = $ResourceName
                            "Percentage of usage" = $ResourcePercentageUsage
                        }
               
                        $ResourceUsageOverQuota = New-Object PSObject -Property $Properties
                        $VMUsageBelowQuota += $ResourceUsageOverQuota
                    }
                }
            }
        }
    
        if ($CheckStorageUsage -eq $true) {
            $NetworkUsageOverQuota = @()
            $NetworkUsageBelowQuota = @()
            $NetworkUsage = Get-AzureRMNetworkUsage -Location $Location
            foreach ($NetworkResource in $NetworkUsage) {
                $ResourceName = $NetworkResource.Name.LocalizedValue
                $ResourceUsageLimit = $NetworkResource.Limit
                $ResouceUsageCurrentStatus = $NetworkResource.CurrentValue
                if ($ResourceUsageLimit -ne 0) {
                    $ResourcePercentageUsage = [Math]::Round(($ResouceUsageCurrentStatus / $ResourceUsageLimit) * 100)
                    if ($ResourcePercentageUsage -gt $PercentageUsageLimit) {
                        $Properties = @{
                            "Resource Name"      = $ResourceName
                            "Percentage of usage" = $ResourcePercentageUsage
                        }
               
                        $ResourceUsageOverQuota = New-Object PSObject -Property $Properties
                        $NetworkUsageOverQuota += $ResourceUsageOverQuota
                    }
                    else {
                        $Properties = @{
                            "Resource Name"      = $ResourceName
                            "Percentage of usage" = $ResourcePercentageUsage
                        }
               
                        $ResourceUsageOverQuota = New-Object PSObject -Property $Properties
                        $NetworkUsageBelowQuota += $ResourceUsageOverQuota
                    }
                }
            }    
        }
    }

    end {
        if ($StoragePercentageUsage -gt $PercentageUsageLimit) {
            Write-Host "Storage usage over quota limit" -ForegroundColor Red
            foreach($stgusage in $StorageUsageArray){
                [console]::ForegroundColor="red"; $_;
            
            }
            $StorageUsageArray | Format-Table
        }
        else{
            Write-Host "Storage usage below quota limit" -ForegroundColor Green
            foreach($stgusage in $StorageUsageArray){
                [console]::ForegroundColor="green"; $_;
            
            }
            $StorageUsageArray | Format-Table
        }
        

        Write-Host "VM usage over quota limit" -ForegroundColor Red
        foreach($vmusage in $VMUsageOverQuota){
            [console]::ForegroundColor="red"; $_;
        
        }
        $VMUsageOverQuota | Format-Table

        Write-Host "VM usage over below quota limit" -ForegroundColor Green
        foreach($vmusage in $VMUsageBelowQuota){
            [console]::ForegroundColor="green"; $_;
        
        }
        $VMUsageBelowQuota | Format-Table

        Write-Host "Network usage over quota limit" -ForegroundColor Red 
        foreach($networkusage in $NetworkUsageOverQuota){
            [console]::ForegroundColor="red"; $_;
        
        }
        $NetworkUsageOverQuota | Format-Table
        Write-Host "Network usage below quota limit" -ForegroundColor Green
        foreach($networkusage in $NetworkUsageBelowQuota){
            [console]::ForegroundColor="green"; $_;
        
        }
        $NetworkUsageBelowQuota | Format-Table
    }
}
Example of usage:
Get-AzureQuotaLimit -Location westeurope -PercentageUsageLimit 30 -CheckStorageUsage $true -CheckVMResourcesUsage $true -CheckNetworkResourcesUsage $true

I hope it will be usefull for some of you 😉
Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.