1

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"
jrd1989
  • 628
  • 10
  • 35
  • what specifically speaks against the lazy way to run the game X times? – djdomi Oct 09 '21 at 08:26
  • 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 – jrd1989 Oct 11 '21 at 18:38
  • 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 Oct 12 '21 at 04:13
  • but i would recommand to join stackoverflow since this is a programm action and not a support case for serverfault – djdomi Oct 12 '21 at 05:45
  • @jrd1989, did you finally get the answer or updated code for your original question? please share it here :-) – Senior Systems Engineer Aug 24 '22 at 01:49
  • Never got a solid solution to this. The project I was on ended. – jrd1989 Sep 11 '22 at 15:02

0 Answers0