从所有资源组中获取特定的标签名称及其值

Get a specific tag name and its value from all resource groups

我需要在我订阅的 excel sheet 中获取标签名称“SquId”及其所有资源组的值。我在下面尝试 code.Its 给了我所有可用的标签。但我只需要将特定标签放入 excel 。一旦我将所有内容放入 excel.

Select-AzureRmSubscription -SubscriptionName $SubscriptionName

$results = @()

$TagsAsString = ""

$datetime = Get-Date -Format "dd-MM-yy-hh-mm"

#Getting all Azure Resource

$resources = Get-AzureRmResource

foreach ($resource in $resources) {

#Fetching Tags

$Tags = $resource.Tags

#Checkign if tags is null or have value

if ($Tags -ne $null) {

$Tags.GetEnumerator() | % { $TagsAsString += $_.Key + ":" + $_.Value + ";" }

}

else {

$TagsAsString = "NULL"

}

#Adding to Results

$details = @{

ResourceGroup = $resource.ResourceGroupName

Resource = $resource.Name

Tags = $TagsAsString

}

$results += New-Object PSObject -Property $details

#Clearing Variable

$TagsAsString = ""

}

$OutputPathWithFileName = $OutputCSVFilePath + "\Tags-" + $SubscriptionName + "-" + $datetime + ".csv"

$results | Select-Object -Property ResourceGroup, Resource, Tags | export-csv -Path $OutputPathWithFileName -NoTypeInformation

}

Azure RM(旧):

Get-AzureRmTag  -Name "SquId" -Detailed

阿兹:

Get-AzTag  -Name "SquId" -Detailed

您可以使用下面的脚本。

$results = @()
$groups = Get-AzureRmResourceGroup 
foreach($group in $groups){
    if($group.Tags.SquId -ne $null){
        $results += New-Object PSObject -Property @{
                    ResourceGroupName = $group.ResourceGroupName;
                    SquId = $group.Tags.SquId
                }
       }
}
$results | Export-Csv -Path C:\Users\joyw\Desktop\tag.csv -NoTypeInformation