如何从多个 Azure 服务中删除标签?

How to delete tags from multiple azure services?

所以标题中的问题:如何从多个Azure服务中删除标签? 它可以与 PowerShell 或 GUI.Thanks)

您可以使用 powershell 来完成:

Remove-AzureRmTag [-Name] <String> [[-Value] <String[]>] [-PassThru] [-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>] 

A​​zure 门户 UI 可用于编辑和删除资源中的标签。它还可以使用 Azure PowerShell SDK 或 Azure CLI 命令行工具编写脚本以自动执行该过程。

Remove-AzureRmTag PowerShell 命令可用于从所有资源中删除特定标记。下面是从所有资源中删除名为 "Department" 的标签的示例:

Remove-AzureRmTag -Name "Department"

可在此处找到有关 Azure PowerShell SDK 的更多文档:https://docs.microsoft.com/en-us/powershell/module/azurerm.tags/remove-azurermtag?view=azurermps-6.13.0

使用 Remove-AzureRmTag 或现在使用 Remove-AzTag 只会删除预定义的标签和值,不会从资源资源中删除该标签。此脚本将在多个订阅中找到所有标记为 department:accounting 的资源,并从资源中删除该标记。

$subs = Get-AzSubscription

$tagname = "department"
$tagvalue = "accounting"

$subs | % {
    Set-AzContext $_
    $rs = Get-AzResource -TagName $tagname -TagValue $tagvalue
    $rs | % {
        $_.Tags.Remove($tagname)
        $_ | Set-AzResource -Force
    }  
}

# That's a method that deletes the tag from your resource.
function Remove-TagsFromResource ($tagname, $tagvalue) {
    $resources = Get-AzResource -TagName $tagname -TagValue $tagvalue

    foreach ($resource in $resources) {
        Write-Host "Removing tag from " $resource.Name
        $deletedtag = @{"$tagname" = "$tagvalue" }
        Update-AzTag -ResourceId $resource.ResourceId -Tag $deletedtag -Operation Delete
        Write-Host "Tag removed from " $resource.Name -ForegroundColor Green
    }
}

# This is how you call the method
Remove-TagsFromResource -tagName "Key" -tagvalue "Value"

试试这个。已测试并有效。

Remove-AzureRmTag/Remove-AzTag 仅在没有带标签的 resources/resource 组时适用于订阅。 此外,AzureRM Powershell 模块已弃用,不应再使用。

下面是我编写的用于从订阅中删除标签或标签字典的脚本。

您可以仅使用 TagName 删除标签,这将删除订阅中具有该标签的所有资源上该标签的所有实例。如果您为脚本提供 TagName 和 TagValue,您可以删除具有特定值的特定标签。如果将对象传递给脚本,您将删除对象中列出的所有键值对。

如您所见,您现在可以使用标签对象过滤 Get-AzResource based on a tag name or tag name + value. At the moment you cant only filter Get-AzResourceGroup

使用 Update-AzTag 方法的好处是您只需要具有读取权限的用户和 Microsoft。Resources/tags/write 在您操作的范围内。

这已通过 Az Powershell 5.1 版测试

[CmdletBinding()]
param (
    [Parameter(Mandatory)]
    [string]
    $SubscriptionName,

    [Parameter(ParameterSetName = "TagName", Mandatory)]
    [Parameter(ParameterSetName = "TagValue", Mandatory)]
    [string]
    $TagName,

    [Parameter(ParameterSetName = "TagValue", Mandatory)]
    [string]
    $TagValue,

    [Parameter(ParameterSetName = "TagObject", Mandatory)]
    [object]
    $Tag
)

Set-AzContext -SubscriptionName $SubscriptionName -ErrorAction:Stop

switch ($PSCmdlet.ParameterSetName) {
    "TagName" {
        Get-AzResourceGroup | Where-Object { $null -ne $_.Tags -and $_.Tags.ContainsKey("$TagName") } |  ForEach-Object {
            $_.Tags.Remove("$TagName")
            Update-AzTag -ResourceId $_.ResourceId -Tag $_.Tags -Operation Replace
        }

        Get-AzResource -TagName "$TagName" | ForEach-Object {
            $_.Tags.Remove("$TagName")
            Update-AzTag -ResourceId $_.Id -Tag $_.Tags -Operation Replace
        }
    }
    "TagValue" {
        Get-AzResourceGroup -Tag @{"$TagName" = "$TagValue" } | ForEach-Object {
            Update-AzTag -ResourceId $_.ResourceId -Tag @{"$TagName" = "$TagValue" } -Operation Delete
        }

        Get-AzResource -Tag @{"$TagName" = "$TagValue" } | ForEach-Object {
            Update-AzTag -ResourceId $_.Id -Tag @{"$TagName" = "$TagValue" } -Operation Delete
        }
    }
    "TagObject" {
        Get-AzResourceGroup -Tag $Tag | ForEach-Object {
            Update-AzTag -ResourceId $_.ResourceId -Tag $Tag -Operation Delete
        }

        Get-AzResource -Tag $Tag | ForEach-Object {
            Update-AzTag -ResourceId $_.Id -Tag $Tag -Operation Delete
        }
    }
}