Reputation: 71
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.
Upvotes: 3
Views: 8440
Reputation: 51
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)"
}
Upvotes: 5
Reputation: 23141
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 "--------------------------------------------"
}
Upvotes: 1