将标签从资源组应用于资源的脚本

时间:2018-12-20 23:04:53

标签: azure powershell

尝试将在资源组级别设置的标签应用于资源组内的所有资源(所有资源)。

在线找到脚本并对其进行最少的修改。

应确保标签NAME和VALUE均基于资源组设置 (例如:如果资源组的标签名称为“ ABCD”且标签值为“ 1234”,并且资源组下方的资源的标签为“ ABCD”且标签值为“ 4567”,则应使用“ 1234”覆盖该值) 应该在所有资源上设置一个标签。我们知道这个名字,但我们不知道它的值。

我注意到运行需要很长时间。包含10个资源的资源组可能需要1-2分钟才能运行脚本

有什么想法或建议吗?

#List all Resources within the Subscription
$Resources = Get-AzureRmResource

#For each Resource apply the Tag of the Resource Group
Foreach ($resource in $Resources)
{
$Rgname = $resource.Resourcegroupname

$resourceid = $resource.resourceId
$RGTags = (Get-AzureRmResourceGroup -Name $Rgname).Tags

$resourcetags = $resource.Tags

If ($resourcetags -eq $null)
    {
        Write-Output "---------------------------------------------"
        Write-Output "Applying the following Tags 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 "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
    }   
}

该脚本也应该做一些事情,我不确定该脚本是否应该做。

  1. 如果资源已经有15个标签,它将不会覆盖标签 具有资源组级别标签;它只会跳过它
  2. 只有一个标签,而不是将所有标签从资源组复制到资源。我们是否可以将逻辑放在资源组中,例如,如果该资源组具有“ ABCDEFG”标签,它将复制它?还有其他标签吗?
  3. 也许可以加快速度,是否可以仅检查资源级别上的标记名称和值是否与资源组级别上的标记匹配,而如果已经匹配则不覆盖它。我怀疑这是花费时间的写操作,而没有读取标签的情况。

1 个答案:

答案 0 :(得分:0)

这应该使您非常接近想要的东西

$tagName = "ABCD"
$tagFallbackValue = "123"

$subscriptionId = "ewn3k4l4jh32jæ42æ3lj4æl12j4"

Connect-AzureRmAccount

$sub = Get-AzureRmSubscription -SubscriptionId $subscriptionId

Set-AzureRmContext -SubscriptionObject $sub

$resGroups = Get-AzureRmResourceGroup

foreach ($resGroup in $resGroups) {
    #Some ResourceGroups might not have tags defined at all.
    if($null -eq $resGroup.Tags) {
        $Tag = @{}
        $null = $Tag.Add($tagName, $tagFallbackValue)

        Set-AzureRmResourceGroup -Tag $Tag -Id $resGroup.ResourceId
    }#Some ResourceGroups might have tags but missing ours.
    elseif ($resGroup.Tags.ContainsKey($tagName) -eq $false) {
        $Tag = $resGroup.Tags
        $null = $Tag.Add($tagName, $tagFallbackValue)

        Set-AzureRmResourceGroup -Tag $Tag -Id $resGroup.ResourceId
    }#We need to test those that have our tag, if they have the desired tag value
    else {
        if($resGroup.Tags.$tagName -ne $tagFallbackValue) {
            $Tag = $resGroup.Tags
            $Tag.$tagName = $tagFallbackValue

            Set-AzureRmResourceGroup -Tag $Tag -Id $resGroup.ResourceId
        }
    }

    $tagValue = $resGroup.Tags.$tagName
    $resGroupName = $resGroup.ResourceGroupName

    #Some resources might already have our tag, find them and test if they have the correct value
    $resHasTag = Get-AzureRmResource -ResourceGroupName $resGroupName | Where-Object {$null -ne $_.tags -and $_.Tags.ContainsKey($tagName) -eq $true}
    foreach ($res in $resHasTag) {
        if($res.Tags.$tagName -ne $tagValue) {
            $Tag = $res.Tags
            $Tag.$tagName = $tagValue

            Set-AzureRmResource -ResourceId $res.ResourceId -Tag $Tag -Force
        }
    }

    #Some resources might not have tags defined at all.
    $resNoTags = Get-AzureRmResource -ResourceGroupName $resGroupName | Where-Object {$null -eq $_.tags -or $_.tags.Count -lt 1}
    foreach ($res in $resNoTags) {
        $Tag = @{}
        $null = $Tag.Add($tagName, $tagValue)
        Set-AzureRmResource -ResourceId $res.ResourceId -Tag $Tag -Force
    }

    #We need to find all resources that is missing our tag and have less than 15 tags
    $resOtherTags = Get-AzureRmResource -ResourceGroupName $resGroupName | Where-Object {$null -ne $_.tags -and $_.tags.Count -lt 15 -and $_.Tags.ContainsKey($tagName) -eq $false}
    foreach ($res in $resOtherTags) {
        $Tag = $res.Tags
        $null = $Tag.Add($tagName, $tagValue)

        Set-AzureRmResource -ResourceId $res.ResourceId -Tag $Tag -Force
    }
}

更新版本符合新要求

$tagName = "COSTCODE"

Connect-AzureRmAccount

$subscriptions = Get-AzureRmSubscription

foreach ($sub in $subscriptions) {
    Set-AzureRmContext -SubscriptionObject $sub

    $resGroups = Get-AzureRmResourceGroup

    foreach ($resGroup in $resGroups) {
        #Some ResourceGroups might not have tags defined at all, we skip those.
        if ($null -eq $resGroup.Tags) { continue }

        #Some ResourceGroups might have tags but missing ours, we skip those.
        elseif ($resGroup.Tags.ContainsKey($tagName) -eq $false) { continue }
        #We need to test those that have our tag, if they have the desired tag value

        $tagValue = $resGroup.Tags.$tagName
        $resGroupName = $resGroup.ResourceGroupName

        #Some resources might already have our tag, find them and test if they have the correct value
        $resHasTag = Get-AzureRmResource -ResourceGroupName $resGroupName | Where-Object {$null -ne $_.tags -and $_.Tags.ContainsKey($tagName) -eq $true}
        foreach ($res in $resHasTag) {
            if ($res.Tags.$tagName -ne $tagValue) {
                $Tag = $res.Tags
                $Tag.$tagName = $tagValue

                Set-AzureRmResource -ResourceId $res.ResourceId -Tag $Tag -Force
            }
        }

        #Some resources might not have tags defined at all.
        $resNoTags = Get-AzureRmResource -ResourceGroupName $resGroupName | Where-Object {$null -eq $_.tags -or $_.tags.Count -lt 1}
        foreach ($res in $resNoTags) {
            $Tag = @{}
            $null = $Tag.Add($tagName, $tagValue)
            Set-AzureRmResource -ResourceId $res.ResourceId -Tag $Tag -Force
        }

        #We need to find all resources that is missing our tag and have less than 15 tags
        $resOtherTags = Get-AzureRmResource -ResourceGroupName $resGroupName | Where-Object {$null -ne $_.tags -and $_.tags.Count -lt 15 -and $_.Tags.ContainsKey($tagName) -eq $false}
        foreach ($res in $resOtherTags) {
            $Tag = $res.Tags
            $null = $Tag.Add($tagName, $tagValue)

            Set-AzureRmResource -ResourceId $res.ResourceId -Tag $Tag -Force
        }
    }
}