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"
0 Answers