3

I can assign and view a "tag" value for my various Azure subscriptions in the Azure Portal.
However, when I query those subscriptions with PowerShell, I cannot find a property relating to "tags." This seems rather bizzare since "Tags" are listed as a property of all PowerShell ResourceGroup objects and the Resources themselves have a "Tags" property also.
Why can I not query for "Tags" at the subscription level if I can assign and view them via the Azure Portal? There must be a way.

F.S.
  • 71
  • 1
  • 4
  • Have you tried [Get-AzResource](https://learn.microsoft.com/en-us/powershell/module/az.resources/get-azresource?view=azps-3.4.0)? It has a `-TagName` and a `-TagValue` parameter. – Olaf Feb 11 '20 at 01:36
  • you can try to use Azure rest api to get it : https://learn.microsoft.com/en-us/rest/api/resources/tags/getatscope#get-tags-on-a-subscription – Jim Xu Feb 12 '20 at 07:16

2 Answers2

3

You can get the tags using Get-AzTag. ResourceId for the subscription is /subscriptions/<subscriptionId>

Replace <subscriptionName> in the following with the name of your subscription

$subscription = Get-Subscription -SubscriptionName <subscriptionName>
$tags = Get-AzTag -ResourceId /subscriptions/$subscription

Example: Tags output example

You Get the value of the tag by

$tags.Properties.TagsProperty['<TagKey>']

Example of getting tag value: Get tag value when key is known

If you want to iterate through the tags, you can do something like this

foreach($tagKey in $tags.Properties.TagsProperty.Keys) {
  # $tagKey contains the tag key
  $tagValue = $tags.Properties.TagsProperty[$tagKey]
  Write-Host "$($tagKey):$($tagValue)"
}

Example script:


param (
    [Parameter(Mandatory = $false)]
    [string] $SubscriptionName,

    [Parameter(Mandatory = $false)]
    [PSCredential] $Credential
)

if ($Credential) {
    [void] (Connect-AzAccount -Credential $Credential)
}
else {
    [void] (Connect-AzAccount)
}

$subscription = Get-AzSubscription -SubscriptionName $SubscriptionName
if (!$subscription) {
    Write-Output "No subscription named '$($SubscriptionName) was found'"
    exit
}

$tags = Get-AzTag -ResourceId /subscriptions/$subscription

foreach($tagKey in $tags.Properties.TagsProperty.Keys) {
    $tagValue = $tags.Properties.TagsProperty[$tagKey]
    Write-Host "$($tagKey):$($tagValue)"
}
Coreskov
  • 31
  • 3
1

According to my test, we can use the following rest api to get the tags of Azure subscription. For more details, please refer to https://learn.microsoft.com/en-us/rest/api/resources/tags/getatscope#get-tags-on-a-subscription.

GET https://management.azure.com/subscriptions/<subscription id>/providers/Microsoft.Resources/tags/default?api-version=2019-10-01

For example

$tenantId="<tenant id>"
Connect-AzAccount -Tenant $tenantId


$resource="https://management.core.windows.net/"

$context=Get-AzContext

$token=$context.TokenCache.ReadItems() |Where-Object { ($_.TenantId -eq $tenantId) -and ($_.Resource -eq $resource)  }
$accesstoken=$token.AccessToken

# get the subscription in the tenant
$subs = Get-AzSubscription -TenantId $tenantId

foreach($sub in $subs){
  $url = "https://management.azure.com/subscriptions/$sub/providers/Microsoft.Resources/tags/default?api-version=2019-10-01"
  $result = Invoke-RestMethod -Uri $url  -Method Get -Headers @{"Authorization" = "Bearer $accesstoken"}

$result.properties 
Write-Host "--------------------------------------------"

}

enter image description here

Jim Xu
  • 21,610
  • 2
  • 19
  • 39