Azure-Tags标记
实现标记自动化
所有资源继承资源组的tag
1.利用Azure policy中的“Append tag and its default value”, 指定tag Name为application, tag value 为undefined
至此之后你所有建立的azure resource 都会在创建之初就带有这个预设的tag
2.建立资源组时做好tagging
3. 利用azure automation的runbook去遍历所有带有预设tag的resource, 并将对应的resource group的标签应用在其自身。
$connectionName = "AzureRunAsConnection" try { $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName "Logging in to Azure..." Add-AzureRmAccount ` -ServicePrincipal ` -TenantId $servicePrincipalConnection.TenantId ` -ApplicationId $servicePrincipalConnection.ApplicationId ` -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint ` -EnvironmentName AzureChinaCloud } catch { if (!$servicePrincipalConnection) { $ErrorMessage = "Connection $connectionName not found." throw $ErrorMessage } else{ Write-Error -Message $_.Exception throw $_.Exception } } Select-AzureRmSubscription -Subscription SubscriptionName $Resources = (Get-azurermresource -Tag @{ Application="undefined"}) Foreach ($resource in $Resources) { $Rgname = $resource.Resourcegroupname $resourceid = $resource.resourceId $RGTags = (Get-AzureRmResourceGroup -Name $Rgname).Tags $resourcetags = $resource.Tags $RGTagFinal = @{} $RGTagFinal = $RGTags Foreach ($resourcetag in $resourcetags.GetEnumerator()) { If ($RGTags.Keys -inotcontains $resourcetag.Key) { Write-Output "------------------------------------------------" Write-Output "Keydoesn't exist in RG Tags adding to Hash Table" $resourcetag Write-Output "------------------------------------------------" $RGTagFinal.Add($resourcetag.Key,$resourcetag.Value) } } Write-Output "---------------------------------------------" Write-Output "Applying the following Tags to $($resourceid)" $RGTagFinal Write-Output "---------------------------------------------" $Settag = Set-AzureRmResource -ResourceId $resourceid -Tag $RGTagFinal -Force }