我需要使用 PowerShell 根据标签值和名称获取所有订阅中的存储帐户详细信息和大小。
我正在使用下面的 Get-Azstorageaccount 模块来获取详细信息,该详细信息为我提供带有标签的输出,但我想修改脚本以过滤掉特定标签名称和值以及存储帐户大小。
# 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."
}
尝试以下:
# Authenticate to Azure
Connect-AzAccount -Tenant 'xxxxx'-UseDeviceAuthentication
# Get all subscriptions in the current tenant
$subscriptions = Get-AzSubscription
# Define the Azure tag name
$tagName = "Owner"
$tagValue = "wenbo"
$storageAccountSkuName = "Standard_RAGRS"
$storageAccounts = @()
foreach ($subscription in $subscriptions) {
Write-Output "Processing subscription $($subscription.Name)..."
# Set the current subscription context
Select-AzSubscription -SubscriptionId $subscription.Id | Out-Null
try {
$res = Get-AzResource -ResourceType 'microsoft.storage/storageaccounts' -ExpandProperties | Where-Object {$_.Tags.Keys -contains $tagName -and $_.Tags[$tagName] -eq $tagValue} | Where-Object {$_.Sku.Name -eq $storageAccountSkuName} | Select-Object -Property ResourceGroupName, Name,Location, Tags, ResourceType, @{n = 'Size'; e= {$_.Sku.Name}}, @{n = 'CreationTime'; e={$_.Properties.creationTime}}
$storageAccounts += $res
}
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."
}