I have an Azure account for development and testing purposes that I want to keep as clean as possible. Part of my routine is clearing out unneeded blobs in the Storage Accounts. To do this using the Portal UI would take a long time… so PowerShell to the rescue.
I thought there would be a quick like piped command something like this:
Get-AzureStorageAccount | Get-AzureStorageContainer | Get-AzureStorageBlob
But no such luck. There’s extra steps to access the Storage Key and to create an Azure Storage Context object needed.
# optional!
$PreviousVerbosePreference = $VerbosePreference
$PreviousWarningPreference = $WarningPreference
$VerbosePreference = "SilentlyContinue"
$WarningPreference = "SilentlyContinue"
# if not alreay logged in to your Azure account...
# Add-AzureAccount
# if more than one subscription
# Select-AzureSubscription -SubscriptionName ????
Get-AzureStorageAccount |
foreach {
$acct = $_.label; $storageKey = (Get-AzureStorageKey -StorageAccountName $acct ).Primary;
$ctx = New-AzureStorageContext -StorageAccountName $acct
-StorageAccountKey $storageKey;
Get-AzureStorageContainer -Container * -Context $ctx } |
foreach { $container = $_.Name; $_ } |
Get-AzureStorageBlob |
Select {$_.context.StorageAccountName}, {$container}, name,blobtype,length |
Format-Table -autosize
# optional!
$VerbosePreference = $PreviousVerbosePreference
Select @{label="Storage Account";expression={$_.context.StorageAccountName}},
@{label="Container";expression={$container}},
name,
blobtype,
@{label="Bytes";expression={"{0,20:N0}" -f $_.length}} |
$WarningPreference = $PreviousWarningPreference
And if you like pretty columns then replace the Select line with this:
Select @{label="Storage Account";expression={$_.context.StorageAccountName}},
@{label="Container";expression={$container}},
name,
blobtype,
@{label="Bytes";expression={"{0,20:N0}" -f $_.length}} |
There’s got to be a better way to do this. So post any better solution as a comment!
Total storage?
Replace the Format-Table line with “Measure-Object -Property length -Sum” and you can get a file count and total bytes. (Don’t combine this with the “pretty columns” change!)
.