如何在 azure PowerShell 中修复此错误:"Can not remove tag/tag value because it's being referenced by other resources."

How to fix this error in azure PowerShell : "Can not remove tag/tag value because it's being referenced by other resources."

我想从 Microsoft Azure 中的大量虚拟机中删除标签。 但我收到此错误:无法删除 tag/tag 值,因为它正被其他资源引用。我需要做什么以及如何解决此错误???

Remove-AzureRmTag -Name "sada" 

此代码已用于从我的所有 Azure 虚拟机中删除 sada

这意味着此标签正被 Azure 中的某些资源使用,您只能使用此 cmdlet 删除未使用的标签。所以你唯一的选择是从现有资源中删除所有这些标签(你可以为此使用一个相当简单的 powershell 脚本,或者只是从门户中进行大量标记)。然后你可以 运行 这个 cmdlet。

像这样:

$res = Get-AzResource -ErrorAction SilentlyContinue
$res.ForEach{
    if ( $_.tags.ContainsKey('sada') ) {
        $_.tags.Remove('sada')
    }
    $_ | Set-AzResource -Tags $_.tags
}

您不能删除当前应用于资源或资源组的标记或值。在使用 Remove-AzTag 之前,请使用 Set-AzResourceGroup cmdlet 的 Tag 参数从资源或资源组中删除标记或值。 https://docs.microsoft.com/en-us/powershell/module/az.resources/remove-aztag?view=azps-4.8.0#description

解决方法很简单:

Update-AzTag -ResourceId $resource.id -Tag $removeTags -Operation Delete