Search code examples
azurepowershellazure-storage-account

How do I fetch consumed size of all Azure storage accounts in a tenant


I have a requirement to fetch consumed data in all storage accounts in a tenant for creating a dashboard.

I might need a PowerShell script where we can get this data into a csv file ! This is to display the date into our own dashboard.


Solution

  • How do I fetch consumed size of all Azure storage accounts in a tenant?

    You can use the below PowerShell script to get the used capacity (consumed size) of all Azure Storage accounts in MB.

    Script:

    Connect-AzAccount -Tenant 'xxxx-xxxx-xxxx-xxxx'
    
    $sub = Get-AzSubscription | select Name
    
    $sub | foreach { 
        Set-AzContext -SubscriptionId $_.Name
    
        $RGs = Get-AzResourceGroup | select ResourceGroupName
        $results = @()
    
        $RGs | foreach {
            $CurrentRG = $_.ResourceGroupName
            $StorageAccounts = Get-AzStorageAccount -ResourceGroupName $CurrentRG | select StorageAccountName
            Write-Host "Processing storage accounts in resource group $CurrentRG..."
            $StorageAccounts | foreach {
                $StorageAccount = $_.StorageAccountName
                $CurrentSAID = (Get-AzStorageAccount -ResourceGroupName $CurrentRG -AccountName $StorageAccount).Id
                $usedCapacity = (Get-AzMetric -ResourceId $CurrentSAID -MetricName "UsedCapacity" -WarningAction SilentlyContinue).Data
                $usedCapacityInMB = $usedCapacity.Average / 1024 / 1024
                $results += [PSCustomObject]@{
                    StorageAccountName = $StorageAccount
                    UsedCapacityInMB = $usedCapacityInMB
                    ResourceGroupName = $CurrentRG
                    Subscription = $_.Name
                }
            }
            Write-Host "Finished processing storage accounts in resource group $CurrentRG."
        }
    
        $results | Export-Csv -Path "output_$($_.Name).csv" -NoTypeInformation
    }
    

    Output: enter image description here

    Reference: Get-AzMetric (Az.Monitor) | Microsoft Learn