Score:0

Calculate Size of Azure Storage Accounts & Containers Using PowerShell

cn flag

I have limited knowledge of PowerShell but I'd like to calculate the total size (in GB) of each storage account, or each container in my storage accounts. I have multiple storage accounts and containers in multiple resource groups.

I'm having a hard time putting together a script that pulls all storage accounts and containers since I have more than one resource group. I found the script below that works fine but it requires entering the name of single storage account and resource group.

Ideally, I want to be able to select all storage accounts in my subscription and not be forced to enter individual storage account name and resource groups. I'd appreciate any help/suggestions with this, thanks!

# Connect to Azure
Connect-AzureRmAccount

# Static Values for Resource Group and Storage Account Names
$resourceGroup = "RGP-01"
$storageAccountName = "storagestg3"

# Get a reference to the storage account and the context
$storageAccount = Get-AzureRmStorageAccount `
-ResourceGroupName $resourceGroup `
-Name $storageAccountName
$ctx = $storageAccount.Context

# Get All Blob Containers
$AllContainers = Get-AzureStorageContainer -Context $ctx
$AllContainersCount = $AllContainers.Count
Write-Host "We found '$($AllContainersCount)' containers. Processing size for each one"

# Zero counters
$TotalLength = 0
$TotalContainers = 0

# Loop to go over each container and calculate size
Foreach ($Container in $AllContainers){
$TotalContainers = $TotalContainers + 1
Write-Host "Processing Container '$($TotalContainers)'/'$($AllContainersCount)'"
$listOfBLobs = Get-AzureStorageBlob -Container $Container.Name -Context $ctx

# zero out our total
$length = 0

# this loops through the list of blobs and retrieves the length for each blob and adds it to the total
$listOfBlobs | ForEach-Object {$length = $length + $_.Length}
$TotalLength = $TotalLength + $length
}
# end container loop

#Convert length to GB
$TotalLengthGB = $TotalLength /1024 /1024 /1024

# Result output
Write-Host "Total Length = " $TotallengthGB "GB"
djdomi avatar
za flag
what specifically speaks against the lazy way to run the game X times?
jrd1989 avatar
cn flag
Do you mean manually enter the name of the storage account each time? If so, thats definitely possible but I have 4 different subscriptions with 50-75 storage accounts each so it would be tedious and time consuming. I'd like to automate the process if possible
djdomi avatar
za flag
i mean `$resourceGroup = "RGP-01"` and `$storageAccountName = "storagestg3"` seems to be the variables that needs to be set, and IMHO you need only to write a script that calls it again with these 2 parameters and this might only be needed one time
djdomi avatar
za flag
but i would recommand to join stackoverflow since this is a programm action and not a support case for serverfault
Senior Systems Engineer avatar
pk flag
@jrd1989, did you finally get the answer or updated code for your original question? please share it here :-)
jrd1989 avatar
cn flag
Never got a solid solution to this. The project I was on ended.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.