I have a requirement to fetch storage account details with size in all subscriptions based on tag value & Name using PowerShell.
I’m using the below Get-Azstorageaccount module to fetch the details which is giving me the output with tags but I want to modify the script to filter out with specific tag name & value also with storage account size.
# Authenticate to Azure
Connect-AzAccount -Tenant 'xxxxx'-UseDeviceAuthentication
# Get all subscriptions in the current tenant
$subscriptions = Get-AzSubscription
# Define the Azure tag name
$tagName = "business"
$tagValue = "xyz"
# Filter storage accounts based on the tag
#$filteredStorageAccounts = $storageAccounts | Where-Object { $_.Tags[$tagName] }
# Initialize an array to store storage account details
$storageAccounts = @()
foreach ($subscription in $subscriptions) {
Write-Output "Processing subscription $($subscription.Name)..."
# Set the current subscription context
Select-AzSubscription -SubscriptionId $subscription.Id | Out-Null
try {
# Get all storage accounts in the subscription (Where-Object { $_.Tags[$tagName] -eq $tagValue })
$storageAccounts += Get-AzStorageAccount | ForEach-Object {
[PSCustomObject]@{
SubscriptionName = $subscription.Name
ResourceGroupName = $_.ResourceGroupName
StorageAccountName = $_.StorageAccountName
Location = $_.PrimaryLocation
AccountType = $_.AccountType
CreationTime = $_.CreationTime
TagName = $_.Tags
}
}
}
catch {
Write-Error "Error fetching storage accounts in subscription $($subscription.Name): $_"
}
}
# Output the results
if ($storageAccounts) {
$storageAccounts | Format-Table -AutoSize
}
else {
Write-Output "No storage accounts found in any subscription."
}
2
Answers
Try below:
You can use the below script to fetch Azure storage account based on
tag
name &used capacity size
also.Script:
Output:
Reference:
Metrics – List – REST API (Azure Monitor) | Microsoft Learn