Created
August 28, 2024 15:10
-
-
Save blakedrumm/becc4c306ea8913c76a776c2310084ec to your computer and use it in GitHub Desktop.
PowerShell script to gather Azure quota and usage data across various services, allowing for region and subscription customization.
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
<# | |
.SYNOPSIS | |
This script gathers Azure quota and usage data for various services like Virtual Machines, Managed Disks, Storage, Networking, etc., for a specified region and outputs the data. | |
Optionally, it exports the data to a CSV file. | |
.DESCRIPTION | |
The script connects to your Azure subscription and retrieves usage and quota information for multiple services such as Virtual Machines, Managed Disks, Storage, and Networking. | |
It sorts the data by usage percentage and displays it in a formatted table. If needed, the data can also be exported to a CSV file. | |
.PARAMETER Location | |
The Azure region for which the quota and usage data is gathered. You can specify any valid Azure region. | |
.PARAMETER SubscriptionId | |
The Azure subscription ID to use for retrieving quota and usage data. If not specified, the script will use the current context. | |
.PARAMETER SubscriptionName | |
The Azure subscription name to use for retrieving quota and usage data. If not specified, the script will use the current context. | |
.NOTES | |
Author: Blake Drumm | |
Contact: [email protected] | |
Website: https://blakedrumm.com | |
Created: August 28th, 2024 | |
.EXAMPLE | |
.\Get-AzureQuotaUsage.ps1 -Location "eastus" -SubscriptionId "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | |
Retrieves the usage and quota data for the East US region under the specified subscription. | |
.EXAMPLE | |
.\Get-AzureQuotaUsage.ps1 -Location "eastus" -SubscriptionName "Visual Studio Enterprise" | |
Retrieves the usage and quota data for the East US region under the specified subscription by name. | |
.EXAMPLE | |
.\Get-AzureQuotaUsage.ps1 -Location "eastus" -SubscriptionId "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" | Export-Csv -Path "AzureQuotaUsage.csv" -NoTypeInformation | |
Retrieves the usage and quota data for the East US region under the specified subscription and exports it to a CSV file. | |
#> | |
param ( | |
[Parameter(Mandatory = $true)] | |
[string]$Location, | |
[Parameter(Mandatory = $false)] | |
[string]$SubscriptionId, | |
[Parameter(Mandatory = $false)] | |
[string]$SubscriptionName | |
) | |
# Connect to Azure and select the subscription | |
# Connect-AzAccount | |
if ($SubscriptionId) | |
{ | |
Set-AzContext -SubscriptionId $SubscriptionId | |
} | |
elseif ($SubscriptionName) | |
{ | |
Set-AzContext -SubscriptionName $SubscriptionName | |
} | |
else | |
{ | |
Write-Host "No subscription specified, using the current context." | |
} | |
$subscription = (Get-AzContext).Subscription | |
$subscriptionId = $subscription.Id | |
$subscriptionName = $subscription.Name | |
# Initialize an empty array to store all the data | |
$allQuotaData = @() | |
# Create a lookup table to format the region names | |
$regionNames = @{ | |
"eastus" = "East US" | |
"eastus2" = "East US 2" | |
"southcentralus" = "South Central US" | |
"westus" = "West US" | |
"westus2" = "West US 2" | |
"centralus" = "Central US" | |
"northcentralus" = "North Central US" | |
"westcentralus" = "West Central US" | |
"canadacentral" = "Canada Central" | |
"canadaeast" = "Canada East" | |
"brazilsouth" = "Brazil South" | |
"brazilse" = "Brazil Southeast" | |
"centralindia" = "Central India" | |
"southindia" = "South India" | |
"westindia" = "West India" | |
"eastasia" = "East Asia" | |
"southeastasia" = "Southeast Asia" | |
"japaneast" = "Japan East" | |
"japanwest" = "Japan West" | |
"koreacentral" = "Korea Central" | |
"koreasouth" = "Korea South" | |
"australiaeast" = "Australia East" | |
"australiasoutheast" = "Australia Southeast" | |
"australiacentral" = "Australia Central" | |
"australiacentral2" = "Australia Central 2" | |
"chinanorth" = "China North" | |
"chinanorth2" = "China North 2" | |
"chinaeast" = "China East" | |
"chinaeast2" = "China East 2" | |
"francecentral" = "France Central" | |
"francesouth" = "France South" | |
"germanynorth" = "Germany North" | |
"germanywestcentral" = "Germany West Central" | |
"norwayeast" = "Norway East" | |
"norwaywest" = "Norway West" | |
"swedencentral" = "Sweden Central" | |
"swedensouth" = "Sweden South" | |
"switzerlandnorth" = "Switzerland North" | |
"switzerlandwest" = "Switzerland West" | |
"uaecentral" = "UAE Central" | |
"uaenorth" = "UAE North" | |
"uksouth" = "UK South" | |
"ukwest" = "UK West" | |
"westeurope" = "West Europe" | |
"northeurope" = "North Europe" | |
"southafricanorth" = "South Africa North" | |
"southafricawest" = "South Africa West" | |
"qatarcentral" = "Qatar Central" | |
"polandcentral" = "Poland Central" | |
} | |
# Resolve the formatted region name | |
$formattedRegionName = if ($regionNames.ContainsKey($Location)) { $regionNames[$Location] } | |
else { $Location } | |
# Retrieve usage and quota information for Virtual Machines | |
Write-Host "Gathering VM quota and usage data for region: $formattedRegionName" | |
# Get the resource usage details for the current region (Virtual Machines) | |
$vmUsageDetails = Get-AzVMUsage -Location $Location | |
foreach ($usage in $vmUsageDetails) | |
{ | |
$currentUsagePercent = if ($usage.Limit -gt 0) { [math]::Round(($usage.CurrentValue / $usage.Limit) * 100, 2) } | |
else { 0 } | |
$allQuotaData += [PSCustomObject]@{ | |
SubscriptionName = $subscriptionName | |
SubscriptionId = $subscriptionId | |
Resource = "Virtual Machines" | |
ResourceType = "Microsoft.Compute" | |
QuotaName = $usage.Name.LocalizedValue | |
Region = $formattedRegionName | |
CurrentUsage = $usage.CurrentValue | |
Limit = $usage.Limit | |
UsagePercent = $currentUsagePercent | |
} | |
} | |
# Retrieve usage and quota information for Managed Disks | |
Write-Host "Gathering Managed Disks quota and usage data for region: $formattedRegionName" | |
$diskUsageDetails = Get-AzVMUsage -Location $Location | Where-Object { $_.Name.LocalizedValue -like "*Disk*" } | |
foreach ($disk in $diskUsageDetails) | |
{ | |
$currentUsagePercent = if ($disk.Limit -gt 0) { [math]::Round(($disk.CurrentValue / $disk.Limit) * 100, 2) } | |
else { 0 } | |
$allQuotaData += [PSCustomObject]@{ | |
SubscriptionName = $subscriptionName | |
SubscriptionId = $subscriptionId | |
Resource = "Managed Disks" | |
ResourceType = "Microsoft.Compute" | |
QuotaName = $disk.Name.LocalizedValue | |
Region = $formattedRegionName | |
CurrentUsage = $disk.CurrentValue | |
Limit = $disk.Limit | |
UsagePercent = $currentUsagePercent | |
} | |
} | |
# Retrieve usage and quota information for Storage | |
Write-Host "Gathering Storage quota and usage data for region: $formattedRegionName" | |
$storageUsageDetails = Get-AzStorageUsage -Location $Location | |
foreach ($storage in $storageUsageDetails) | |
{ | |
$currentUsagePercent = if ($storage.Limit -gt 0) { [math]::Round(($storage.CurrentValue / $storage.Limit) * 100, 2) } | |
else { 0 } | |
$allQuotaData += [PSCustomObject]@{ | |
SubscriptionName = $subscriptionName | |
SubscriptionId = $subscriptionId | |
Resource = "Storage" | |
ResourceType = "Microsoft.Storage" | |
QuotaName = $storage.LocalizedName | |
Region = $formattedRegionName | |
CurrentUsage = $storage.CurrentValue | |
Limit = $storage.Limit | |
UsagePercent = $currentUsagePercent | |
} | |
} | |
# Retrieve usage and quota information for Network resources | |
Write-Host "Gathering Network quota and usage data for region: $formattedRegionName" | |
$networkUsageDetails = Get-AzNetworkUsage -Location $Location | |
foreach ($networkUsage in $networkUsageDetails) | |
{ | |
$currentUsagePercent = if ($networkUsage.Limit -gt 0) { [math]::Round(($networkUsage.CurrentValue / $networkUsage.Limit) * 100, 2) } | |
else { 0 } | |
$allQuotaData += [PSCustomObject]@{ | |
SubscriptionName = $subscriptionName | |
SubscriptionId = $subscriptionId | |
Resource = "Network" | |
ResourceType = "Microsoft.Network" | |
QuotaName = $networkUsage.Name.LocalizedValue | |
Region = $formattedRegionName | |
CurrentUsage = $networkUsage.CurrentValue | |
Limit = $networkUsage.Limit | |
UsagePercent = $currentUsagePercent | |
} | |
} | |
# Convert the gathered data into a single table and display it | |
$allQuotaData | Sort-Object UsagePercent -Descending | Format-Table -Property QuotaName, Region, Resource, SubscriptionName, SubscriptionId, CurrentUsage, Limit, UsagePercent -AutoSize | |
# Optionally, export the data to a CSV file | |
# $allQuotaData | Export-Csv -Path "AzureQuotaUsage.csv" -NoTypeInformation | |
<# | |
Copyright (c) Microsoft Corporation. MIT License | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | |
#> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment