How to Programmatically get Azure Storage Accounts Usage

Azure Storage is a powerful and scalable cloud storage solution provided by Microsoft Azure.

Monitoring the usage of your Azure Storage accounts is crucial for optimizing costs and ensuring efficient resource utilization. In this blog post, we will explore how to programmatically retrieve Azure Storage account usage using Azure PowerShell Module.

It is easy to get the Storage Usage for one of the accounts as you can use the GUI but to get the storage across multiple accounts is somewhat difficult.


For getting the same across multiple accounts I thought I can crawl thru each storage account and then thru Blob and add up the length property.

Everything was good until I hit a big storage account that had millions of blobs, it stuck there for a day and half before I had to Kill it.

So how we can get that info, simple answer is instead of using Get-AzStorageContainer followed by Get-AzStorageBlob We have to utilize Get-AzMetric.

Yes, you need to get that data from Azure monitor.


You should have Monitoring Reader role and you can extract the data from Azure Monitor.

Let me share example command so that you can build upon it.

$resourceId = “/subscriptions/$subscriptionId/resourceGroups/$($storageAccount.ResourceGroupName)/providers/Microsoft.Storage/storageAccounts/$($storageAccount.StorageAccountName)”

$metric = (Get-AzMetric -ResourceId $resourceId -MetricName “UsedCapacity” -StartTime “02:00:00” -EndTime “04:00:00”)

$SizeInGB = [Math]::Round($metric.data[0].Average / (1GB), 2)

Metric data contains below output:


$SizeInGB is just a formula to convert the first vale to Gbs.

This is what I have used to fetch the storage data across multiple accounts of a big organization.

I hope this TIP will assist you in building the solution if you are also struggling on how to get this information.

 

Thanks for reading and downloading…

Tech Wizard

 

https://techwizard.cloud

https://syscloudpro.com/

PowerShell Fast Track

One thought on “How to Programmatically get Azure Storage Accounts Usage

  1. Pingback: PowerShell – Report on AZURE Storage Account Usage in Organization | Tech Wizard

Leave a comment