使用 PowerShell 计算 Azure 存储帐户和容器的大小

使用 PowerShell 计算 Azure 存储帐户和容器的大小

我对 PowerShell 的了解有限,但我想计算每个存储帐户的总大小(以 GB 为单位), 或者每个容器在我的存储帐户中。我有多个存储帐户和容器多个资源组

由于我有多个资源组,因此我很难编写一个脚本来提取所有存储帐户和容器。我发现下面的脚本运行良好,但它需要输入单个存储帐户和资源组

理想情况下,我希望能够选择所有存储帐户在我的订阅中不是被迫输入单独的存储帐户名称和资源组。如果您能提供任何帮助/建议,我将不胜感激,谢谢!

# 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"

相关内容