Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Use Powershell to add Tags for Azure resources in bulk

2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

In the process of using Azure, we can apply Tags to Azure resources, so that the metadata logic can be organized into certain categories. Each Tags consists of a name and a value. For example, we can apply the name "Environment" and the value "Production" to all resources in production.

After applying Tags, we can use the Tags name and value to retrieve all resources in the subscription. Using Tags, you can retrieve related resources from different resource groups. This approach is useful when we need to organize resources for billing or management.

It is important to note that when we use TagsTags resources, there are the following restrictions:

Not all resource types support Tags.

Each resource or resource group can have up to 15 Tags name / value pairs. This restriction applies only to Tags that applies directly to resource groups or resources. A resource group can contain many resources, each with 15 Tags name / value pairs. If you need more than 15 values associated with the resource, use the JSON string as the tags value. The JSON string can contain many values applied to a single Tags name. This article shows an example of assigning a JSON string to a Tags.

The Tags name is limited to 512 characters and the tags value is limited to 256 characters. For storage accounts, the Tags name is limited to 128characters and the tags value is limited to 256characters.

The total number of virtual machines is limited to 2048 characters for all Tags names and values.

Tags applied to a resource group is not inherited by resources in that resource group.

Tags cannot be applied to traditional resources such as cloud services.

The label name cannot contain these characters:,%, &,\,?, /

To apply Tags to a resource, the user must have write access to that resource type. To apply Tags to all resource types, use the Contributor role. To apply Tags to only one resource type, use the Contributor role of that resource.

Having said that, let's take a look at how to use Powershell to add Tags for all resources under the resource group in batches:

# Login to Azure Login-AzureRmAccount-Environment AzureChinaCloud # get the resource group name (either individually or all) $Resources = Get-AzureRmResource-ResourceGroupName "ResourceGroupName" # for each resource The tag Foreach ($resource in $Resources) {$Rgname = $resource.Resourcegroupname $resourceid = $resource.resourceId $RGTags = (Get-AzureRmResourceGroup-Name $Rgname) of the application resource group. Tags $resourcetags = $resource.Tags If ($resourcetags-eq $null) {Write-Output "-" Write-Output " Apply the following tag to $($resourceid) "$RGTags Write-Output"-"$Settag = Set-AzureRmResource-ResourceId $resourceid-Tag $RGTagS-Force} Else {$RGTagFinal = @ {} $RGTagFinal = $RGTags Foreach ($resourcetag in $resourcetags.GetEnumerator ()) {If () $RGTags.Keys-inotcontains $resourcetag.Key) {Write-Output "- -" Write-Output "key does not exist in the RG tag to add" $resourcetag Write-Output "to the hash table -"$RGTagFinal.Add ($resourcetag.Key) $resourcetag.Value)}} Write-Output "-" Write-Output "applies the following tag to: $($resourceid)" $RGTagFinal Write-Output "-- -"$Settag = Set-AzureRmResource-ResourceId $resourceid-Tag $RGTagFinal-Force}}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report