Skip to content

Commit 2eabd9c

Browse files
committed
update
1 parent 0dca8bf commit 2eabd9c

File tree

1 file changed

+16
-67
lines changed

1 file changed

+16
-67
lines changed

articles/azure-resource-manager/management/tag-resources.md

Lines changed: 16 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ You apply tags to your Azure resources, resource groups, and subscriptions to lo
1010

1111
For recommendations on how to implement a tagging strategy, see [Resource naming and tagging decision guide](/azure/cloud-adoption-framework/decision-guides/resource-tagging/?toc=/azure/azure-resource-manager/management/toc.json).
1212

13-
You can apply Azure Policies to make sure tagging conventions are maintained for your organization. For more information, see [Assign policies for tag compliance](tag-policies.md).
14-
1513
[!INCLUDE [Handle personal data](../../../includes/gdpr-intro-sentence.md)]
1614

1715
## Required access
@@ -110,10 +108,11 @@ To update the tags for a resource group, use:
110108

111109
```azurepowershell-interactive
112110
$tags = @{"Team"="Compliance"; "Environment"="Production"}
111+
$resourceGroup = Get-AzResourceGroup -Name demoGroup
113112
Update-AzTag -ResourceId $resourceGroup.ResourceId -Tag $tags -Operation Merge
114113
```
115114

116-
To add tags to a subscription.
115+
To add tags to a subscription, use:
117116

118117
```azurepowershell-interactive
119118
$tags = @{"CostCenter"="00123"; "Environment"="Dev"}
@@ -125,10 +124,14 @@ You may have more than one resource with the same name in a resource group. In t
125124

126125
```azurepowershell-interactive
127126
$resource = Get-AzResource -ResourceName sqlDatabase1 -ResourceGroupName examplegroup
128-
$resource | ForEach-Object { Set-AzResource -Tag @{ "Dept"="IT"; "Environment"="Test" } -ResourceId $_.ResourceId -Force }
127+
$resource | ForEach-Object { Update-AzTag -Tag @{ "Dept"="IT"; "Environment"="Test" } -ResourceId $_.ResourceId -Operation Merge }
129128
```
130129

131-
### List by tags
130+
### Inherit tags
131+
132+
To apply tags from a subscription or resource group to the resources, see [Azure Policies - tags](tag-policies.md).
133+
134+
### List tags
132135

133136
To see the existing tags for a *resource group*, use:
134137

@@ -138,7 +141,7 @@ To see the existing tags for a *resource group*, use:
138141

139142
That script returns the following format:
140143

141-
```powershell
144+
```output
142145
Name Value
143146
---- -----
144147
Dept IT
@@ -157,6 +160,8 @@ Or, if you have the resource ID for a resource, you can pass that resource ID to
157160
(Get-AzResource -ResourceId /subscriptions/<subscription-id>/resourceGroups/<rg-name>/providers/Microsoft.Storage/storageAccounts/<storage-name>).Tags
158161
```
159162

163+
### List resources by tag
164+
160165
To get *resources that have a specific tag name and value*, use:
161166

162167
```azurepowershell-interactive
@@ -175,44 +180,9 @@ To get *resource groups that have a specific tag name and value*, use:
175180
(Get-AzResourceGroup -Tag @{ "Dept"="Finance" }).ResourceGroupName
176181
```
177182

178-
To apply all tags from a resource group to its resources, and *not keep existing tags on the resources*, use the following script:
179-
180-
```azurepowershell-interactive
181-
$group = Get-AzResourceGroup -Name examplegroup
182-
Get-AzResource -ResourceGroupName $group.ResourceGroupName | ForEach-Object {Set-AzResource -ResourceId $_.ResourceId -Tag $group.Tags -Force }
183-
```
184-
185-
To apply all tags from a resource group to its resources, and *keep existing tags on resources that aren't duplicates*, use the following script:
186-
187-
```azurepowershell-interactive
188-
$group = Get-AzResourceGroup -Name examplegroup
189-
if ($null -ne $group.Tags) {
190-
$resources = Get-AzResource -ResourceGroupName $group.ResourceGroupName
191-
foreach ($r in $resources)
192-
{
193-
$resourcetags = (Get-AzResource -ResourceId $r.ResourceId).Tags
194-
if ($resourcetags)
195-
{
196-
foreach ($key in $group.Tags.Keys)
197-
{
198-
if (-not($resourcetags.ContainsKey($key)))
199-
{
200-
$resourcetags.Add($key, $group.Tags[$key])
201-
}
202-
}
203-
Set-AzResource -Tag $resourcetags -ResourceId $r.ResourceId -Force
204-
}
205-
else
206-
{
207-
Set-AzResource -Tag $group.Tags -ResourceId $r.ResourceId -Force
208-
}
209-
}
210-
}
211-
```
212-
213183
### Remove tags
214184

215-
You can also set **-Operation** to **Delete** to remove specified tags.
185+
To remove specific tags, use **Update-AzTag** and set **-Operation** to **Delete**.
216186

217187
```azurepowershell-interactive
218188
$removeTags = @{"Project"="ECommerce"; "Team"="Web"}
@@ -231,7 +201,8 @@ Properties :
231201
To remove all tags, use the [Remove-AzTag](/powershell/module/az.resources/remove-aztag) command.
232202

233203
```azurepowershell-interactive
234-
Set-AzResourceGroup -Tag @{} -Name examplegroup
204+
$subscription = (Get-AzSubscription -SubscriptionName "Example Subscription").Id
205+
Remove-AzTag -ResourceId "/subscriptions/$subscription"
235206
```
236207

237208
## Azure CLI
@@ -301,32 +272,10 @@ To append a tag to the existing tags on a resource, use:
301272
az resource update --set tags.'Status'='Approved' -g examplegroup -n examplevnet --resource-type "Microsoft.Network/virtualNetworks"
302273
```
303274

304-
To apply all tags from a resource group to its resources, and *not keep existing tags on the resources*, use the following script:
305-
306-
```azurecli-interactive
307-
jsontags=$(az group show --name examplegroup --query tags -o json)
308-
tags=$(echo $jsontags | tr -d '"{},' | sed 's/: /=/g')
309-
resourceids=$(az resource list -g examplegroup --query [].id --output tsv)
310-
for id in $resourceids
311-
do
312-
az resource tag --tags $tags --id $id
313-
done
314-
```
315-
316-
To apply all tags from a resource group to its resources, and *keep existing tags on resources*, use the following script:
275+
### Inherit tags
317276

318-
```azurecli-interactive
319-
jsontags=$(az group show --name examplegroup --query tags -o json)
320-
tags=$(echo $jsontags | tr -d '"{},' | sed 's/: /=/g')
277+
To apply tags from a subscription or resource group to the resources, see [Azure Policies - tags](tag-policies.md).
321278

322-
resourceids=$(az resource list -g examplegroup --query [].id --output tsv)
323-
for id in $resourceids
324-
do
325-
resourcejsontags=$(az resource show --id $id --query tags -o json)
326-
resourcetags=$(echo $resourcejsontags | tr -d '"{},' | sed 's/: /=/g')
327-
az resource tag --tags $tags$resourcetags --id $id
328-
done
329-
```
330279

331280
If your tag names or values include spaces, you must take a couple of extra steps. The following example applies all tags from a resource group to its resources when the tags may contain spaces.
332281

0 commit comments

Comments
 (0)