Created
March 14, 2021 14:19
-
-
Save iamsunny/8718fb29146363af11da95e5eb82f245 to your computer and use it in GitHub Desktop.
Get Azure Storage Size using PowerShell | Get Azure Storage Capacity using PowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param($resourceGroup, $storageAccountName) | |
# usage | |
# Get-StorageAccountSize -resourceGroup <resource-group> -storageAccountName <storage-account-name> | |
# Connect to Azure | |
Connect-AzureRmAccount | |
# 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" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment