Last active
October 29, 2016 02:24
-
-
Save srpomeroy/f514f679cd80fd48af433f0b38053953 to your computer and use it in GitHub Desktop.
Enumerates through all storage accounts in an Azure Resource Manage subscription and returns the total number of Storage Accounts, Containers and Blobs as well as the total time to execute.
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
$subsciptionId = "<Azure Subscription ID>" | |
Select-AzureRmSubscription -SubscriptionId $subsciptionId | |
$startTime = Get-Date | |
$storageAccounts = Get-AzureRmStorageAccount | |
$totalStorageAccounts = $($storageAccounts.Count) | |
$totalContainers = $null | |
$totalBlobs = $null | |
foreach($storageAccount in $storageAccounts) { | |
Write-Output "Storage Account Name: $($storageAccount.StorageAccountName)" | |
$storageAccountKey = $null; $storageAccountContext = $null | |
$storageAccountKey = ((Get-AzureRmStorageAccountKey -ResourceGroupName $storageAccount.ResourceGroupName -Name $storageAccount.StorageAccountName) | Where-Object KeyName -eq "key1").Value | |
$storageAccountContext = New-AzureStorageContext –StorageAccountName $storageAccount.StorageAccountName -StorageAccountKey $storageAccountKey | |
$storageAccountContainers = Get-AzureStorageContainer -Context $storageAccountContext | |
$totalContainers += $storageAccountContainers.Count | |
if($storageAccountContainers.Count -gt 0) { | |
Write-Output "`tContainer count: $($storageAccountContainers.Count)" | |
foreach($storageAccountContainer in $storageAccountContainers) { | |
$blobs = Get-AzureStorageBlob -Context $storageAccountContext -Container $storageAccountContainer.Name | |
Write-Output "`t$($storageAccountContainer.Name) - Number of blobs: $($blobs.Count)" | |
$totalBlobs += $blobs.Count | |
} | |
Write-Output "" | |
} | |
else { | |
Write-Output "`tContainer count: 0`n`r" | |
} | |
} | |
$endTime = Get-Date | |
Write-Output "Total Storage Accounts: $totalStorageAccounts" | |
Write-Output "Total Containers: $totalContainers" | |
Write-Output "Total Blobs: $totalBlobs" | |
Write-Output "Start Time: $($startTime)" | |
Write-Output "End Time: $(Get-Date)" | |
Write-Output "Total runtime(minutes): $((New-TimeSpan -Start $startTime -End $endTime).TotalMinutes)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment