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

时间:2020-07-06 04:14:19

标签: azure azure-powershell

我需要在我对excel工作表的订阅中获得一个标记名“ SquId”及其所有资源组的值。我尝试了下面的代码,它给了我所有可用的标签。但是我只需要获取特定标签即可使用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

}

2 个答案:

答案 0 :(得分:0)

Azure RM(旧):

Get-AzureRmTag  -Name "SquId" -Detailed

Az:

Get-AzTag  -Name "SquId" -Detailed

答案 1 :(得分:0)

您可以使用下面的脚本。

Date

enter image description here