Skip to content

Commit 0dca8bf

Browse files
committed
update
1 parent 54e9185 commit 0dca8bf

File tree

2 files changed

+43
-50
lines changed

2 files changed

+43
-50
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ms.date: 03/18/2020
77

88
# Assign policies for tag compliance
99

10-
You can use [Azure Policy](../../governance/policy/overview.md) to enforce tagging rules and conventions. By creating a policy, you avoid the scenario of resources being deployed to your subscription that don't comply with the expected tags for your organization. Instead of manually applying tags or searching for resources that aren't compliant, you can create a policy that automatically applies the needed tags during deployment. Tags can also now be applied to existing resources with the new [Modify](../../governance/policy/concepts/effects.md#modify) effect and a [remediation task](../../governance/policy/how-to/remediate-resources.md). The following section shows example policies for tags.
10+
You use [Azure Policy](../../governance/policy/overview.md) to enforce tagging rules and conventions. By creating a policy, you avoid the scenario of resources being deployed to your subscription that don't have the expected tags for your organization. Instead of manually applying tags or searching for resources that aren't compliant, you create a policy that automatically applies the needed tags during deployment. Tags can also now be applied to existing resources with the new [Modify](../../governance/policy/concepts/effects.md#modify) effect and a [remediation task](../../governance/policy/how-to/remediate-resources.md). The following section shows example policies for tags.
1111

1212
## Policies
1313

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

Lines changed: 42 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ To apply tags to resources, the user must have write access to that resource typ
2020

2121
## PowerShell
2222

23+
### Apply tags
24+
2325
Azure PowerShell offers two commands for applying tags - [New-AzTag](/powershell/module/az.resources/new-aztag) and [Update-AzTag](/powershell/module/az.resources/update-aztag). You must have Azure PowerShell 3.6.1 or later to use these commands.
2426

2527
The **New-AzTag** replaces all tags on the resource, resource group, or subscription. When calling the command, pass in the resource ID of the entity you wish to tag.
@@ -34,7 +36,7 @@ New-AzTag -ResourceId $resource.id -Tag $tags
3436

3537
When the command completes, notice that the resource has two tags.
3638

37-
```azurepowershell
39+
```output
3840
Properties :
3941
Name Value
4042
====== =======
@@ -49,7 +51,7 @@ $tags = @{"Team"="Compliance"; "Environment"="Production"}
4951
New-AzTag -ResourceId $resource.id -Tag $tags
5052
```
5153

52-
```azurepowershell
54+
```output
5355
Properties :
5456
Name Value
5557
=========== ==========
@@ -66,7 +68,7 @@ Update-AzTag -ResourceId $resource.id -Tag $tags -Operation Merge
6668

6769
Notice all four tags have been applied to the resource.
6870

69-
```azurepowershell
71+
```output
7072
Properties :
7173
Name Value
7274
=========== ==========
@@ -79,41 +81,44 @@ Properties :
7981
When you set the **-Operation** parameter to **Replace**, the existing tags are replaced by the new set of tags.
8082

8183
```azurepowershell-interactive
82-
$tags = @{"Project"="ECommerce"; "CostCenter"="00123"}
84+
$tags = @{"Project"="ECommerce"; "CostCenter"="00123"; "Team"="Web"}
8385
Update-AzTag -ResourceId $resource.id -Tag $tags -Operation Replace
8486
```
8587

8688
Only the new tags remain on the resource.
8789

88-
```azurepowershell
90+
```output
8991
Properties :
9092
Name Value
9193
========== =========
9294
CostCenter 00123
95+
Team Web
9396
Project ECommerce
9497
```
9598

96-
You can also set **-Operation** to **Delete** to remove a specific tag.
99+
The same commands also work with resource groups or subscriptions. You pass in the identifier for the resource group or subscription you want to tag.
100+
101+
To add a new set of tags to a resource group, use:
97102

98103
```azurepowershell-interactive
99-
$removeTags = @{"Project"="ECommerce"}
100-
Update-AzTag -ResourceId $resource.id -Tag $removeTags -Operation Delete
104+
$tags = @{"Dept"="Finance"; "Status"="Normal"}
105+
$resourceGroup = Get-AzResourceGroup -Name demoGroup
106+
New-AzTag -ResourceId $resourceGroup.ResourceId -tag $tags
101107
```
102108

103-
The specified tag is removed.
109+
To update the tags for a resource group, use:
104110

105-
```azurepowershell
106-
Properties :
107-
Name Value
108-
========== =====
109-
CostCenter 00123
111+
```azurepowershell-interactive
112+
$tags = @{"Team"="Compliance"; "Environment"="Production"}
113+
Update-AzTag -ResourceId $resourceGroup.ResourceId -Tag $tags -Operation Merge
110114
```
111115

112-
To add tags to a *resource without existing tags*, use:
116+
To add tags to a subscription.
113117

114118
```azurepowershell-interactive
115-
$resource = Get-AzResource -ResourceName examplevnet -ResourceGroupName examplegroup
116-
Set-AzResource -Tag @{ "Dept"="IT"; "Environment"="Test" } -ResourceId $resource.ResourceId -Force
119+
$tags = @{"CostCenter"="00123"; "Environment"="Dev"}
120+
$subscription = (Get-AzSubscription -SubscriptionName "Example Subscription").Id
121+
Update-AzTag -ResourceId "/subscriptions/$subscription" -Tag $tags -Operation Merge
117122
```
118123

119124
You may have more than one resource with the same name in a resource group. In that case, you can set each resource with the following commands:
@@ -123,13 +128,7 @@ $resource = Get-AzResource -ResourceName sqlDatabase1 -ResourceGroupName example
123128
$resource | ForEach-Object { Set-AzResource -Tag @{ "Dept"="IT"; "Environment"="Test" } -ResourceId $_.ResourceId -Force }
124129
```
125130

126-
To add tags to a *resource that has existing tags*, use:
127-
128-
```azurepowershell-interactive
129-
$resource = Get-AzResource -ResourceName examplevnet -ResourceGroupName examplegroup
130-
$resource.Tags.Add("Status", "Approved")
131-
Set-AzResource -Tag $resource.Tags -ResourceId $resource.ResourceId -Force
132-
```
131+
### List by tags
133132

134133
To see the existing tags for a *resource group*, use:
135134

@@ -170,36 +169,12 @@ To get *resources that have a specific tag name*, use:
170169
(Get-AzResource -TagName "Dept").Name
171170
```
172171

173-
### Resource group
174-
175172
To get *resource groups that have a specific tag name and value*, use:
176173

177174
```azurepowershell-interactive
178175
(Get-AzResourceGroup -Tag @{ "Dept"="Finance" }).ResourceGroupName
179176
```
180177

181-
182-
183-
184-
185-
Every time you apply tags to a resource or a resource group, you overwrite the existing tags on that resource or resource group. Therefore, you must use a different approach based on whether the resource or resource group has existing tags.
186-
187-
To add tags to a *resource group without existing tags*, use:
188-
189-
```azurepowershell-interactive
190-
Set-AzResourceGroup -Name examplegroup -Tag @{ "Dept"="IT"; "Environment"="Test" }
191-
```
192-
193-
To add tags to a *resource group that has existing tags*, retrieve the existing tags, add the new tag, and reapply the tags:
194-
195-
```azurepowershell-interactive
196-
$tags = (Get-AzResourceGroup -Name examplegroup).Tags
197-
$tags.Add("Status", "Approved")
198-
Set-AzResourceGroup -Tag $tags -Name examplegroup
199-
```
200-
201-
202-
203178
To apply all tags from a resource group to its resources, and *not keep existing tags on the resources*, use the following script:
204179

205180
```azurepowershell-interactive
@@ -235,7 +210,25 @@ if ($null -ne $group.Tags) {
235210
}
236211
```
237212

238-
To remove all tags, pass an empty hash table:
213+
### Remove tags
214+
215+
You can also set **-Operation** to **Delete** to remove specified tags.
216+
217+
```azurepowershell-interactive
218+
$removeTags = @{"Project"="ECommerce"; "Team"="Web"}
219+
Update-AzTag -ResourceId $resource.id -Tag $removeTags -Operation Delete
220+
```
221+
222+
The specified tags are removed.
223+
224+
```output
225+
Properties :
226+
Name Value
227+
========== =====
228+
CostCenter 00123
229+
```
230+
231+
To remove all tags, use the [Remove-AzTag](/powershell/module/az.resources/remove-aztag) command.
239232

240233
```azurepowershell-interactive
241234
Set-AzResourceGroup -Tag @{} -Name examplegroup

0 commit comments

Comments
 (0)