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 }
分类:
Azure
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人