I have this requirement to get storage account details and list storage blobs filtered out by specific container "test". I got this reference code from stackoverflow which will output the container based on tag value. I tried adding the "if" condition to the same script to filter container details which isn’t giving any results, also unsure how to list blobs in the same script filtered by container "test".
I’m expecting the script to output storage account details (filtered by tags), storage account size, container(filtered by specific container) and list blobs from the filtered containers.
Request for more details if the above isn’t enough.
Connect-AzAccount -Tenant 'xxxxx' -UseDeviceAuthentication
# Get all subscriptions
$subscriptions = Get-AzSubscription
# Define the tag key to filter by
$tagKey = "any"
# Initialize an array to store the results
$results = @()
foreach ($subscription in $subscriptions) {
# Set the current subscription context
Set-AzContext -SubscriptionId $subscription.Id
# Get all storage accounts in the current subscription
$storageAccounts = Get-AzStorageAccount | Where-Object { $_.Tags.ContainsKey($tagKey) }
foreach ($storageAccount in $storageAccounts) {
# Get the metrics for the storage account
$resourceId = "/subscriptions/$($subscription.Id)/resourceGroups/$($storageAccount.ResourceGroupName)/providers/Microsoft.Storage/storageAccounts/$($storageAccount.StorageAccountName)"
$uri = "https://management.azure.com$resourceId/providers/Microsoft.Insights/metrics?api-version=2023-10-01&metricnames=UsedCapacity&aggregation=Average"
try {
$response = Invoke-AzRestMethod -Method Get -Uri $uri
$metrics = $response.Content | ConvertFrom-Json
$usedCapacityMetric = $metrics.value | Where-Object { $_.name.value -eq "UsedCapacity" }
if ($usedCapacityMetric) {
$averageCapacity = $usedCapacityMetric.timeseries.data.average | Measure-Object -Sum | Select-Object -ExpandProperty Sum
} else {
$averageCapacity = 0
}
} catch {
Write-Warning "Failed to retrieve metrics for storage account: $($storageAccount.StorageAccountName)"
$averageCapacity = 0
}
$ctx = $storageAccount.Context
$containers = Get-AzStorageContainer -Context $ctx
foreach ($container in $containers) {
if ($container.Name -eq "test") {
$results += [PSCustomObject]@{
#SubscriptionId = $subscription.Id
SubscriptionName = $subscription.Name
ResourceGroup = $storageAccount.ResourceGroupName
StorageAccount = $storageAccount.StorageAccountName
ContainerName = $container.Name
UsedCapacityInBytes = $averageCapacity
TagName = $tagKey
TagValue = $storageAccount.Tags[$tagKey]
}
}
}
}
}
# Output the results
$results | Format-Table -AutoSize
2
Answers
You can use the below script to get the
storage account details (filtered by tags)
,storage account size
,container(filtered by specific container)
andlist blobs
from the filtered containers.Script:
Output:
I see you copied over the script from one of your earlier questions (answered by Venkatesan). But I believe you have trouble fully grasping what it is doing. So your first step should be to understand what it does.
I can try to help you through this, since it would benefit you the most if you learn how to build these scripts yourself. If you are willing, let’s work together in this answer to get you where you want to end.
First up
You say you wanted to only retrieve storage blobs within a specific container. There is no need for you to retrieve all subscriptions. Just use Connect-AzAccount (make sure it’s v3.0.0 or higher so you can select the right subscription right away and you won’t have to set any context yourself).
Second
You use a filter on a tagkey. Try to research what a key-value pair in any programing language means. You will understand why "any" is very unlikely to be a key (might be a value though).
Start building the script
If you followed the first 2 remarks, you will notice you only need the following cmdlets to start building your script:
Connect-AzAccount
andGet-AzStorageAccount
Now it’s time to do some research. Have a look at the documentation here. Try to retrieve just the storageaccount you need (filter early so you won’t have to loop through results if you don’t have to). When you have some results, pipe them through a
Get-Member
(link) and see which properties you are interested in may be hidden. Select the properties you want by piping the results through aSelect-Object
(link). When in doubt I personally useSelect-Object -Property *
when developing since I will get a clear view of which properties actually contain data and what that data is.If you are missing properties you can try to combine data from different cmdlets. Venkatesan showed you how to do that in the script he posted on your previous question.