如何使用 PowerShell 标记 Azure 中的虚拟机

适用于:✔️ Linux VM ✔️ Windows VM ✔️ 灵活规模集 ✔️ 统一规模集

本文介绍如何使用 PowerShell 在 Azure 中标记 VM。 标记是用户定义的键/值对,可直接放置在资源或资源组中。 针对每个资源和资源组,Azure 当前支持最多 50 个标记。 标记可以在创建时放置在资源中或添加到现有资源中。 如果要使用 Azure CLI 标记虚拟机,请参阅如何使用 Azure CLI 标记 Azure 中的虚拟机

请使用 Get-AzVM cmdlet 来查看 VM 的当前标记列表。

Get-AzVM -ResourceGroupName "myResourceGroup" -Name "myVM" | Format-List -Property Tags

如果虚拟机已包含标记,你将会看到呈现为列表格式的所有标记。

若要添加标记,请使用 Set-AzResource 命令。 在通过 PowerShell 更新标记时,标记会作为整体进行更新。 如果要为已经具有标记的资源添加一个标记,则需要包括想要放置在资源中的所有标记。 下面是如何通过 PowerShell Cmdlet 将其他标记添加到资源的示例。

使用 Get-AzResourceTags 属性,将 VM 的所有当前标记分配到 $tags 变量。

$tags = (Get-AzResource -ResourceGroupName myResourceGroup -Name myVM).Tags

若要查看当前标记,请键入该变量。

$tags

输出可能如下所示:

Key           Value
----          -----
Department    MyDepartment
Application   MyApp1
Created By    MyName
Environment   Production

在下面的示例中,我们添加一个名为 Location 且值为 myLocation 的标记。 使用 += 将新的键/值对追加到 $tags 列表。

$tags += @{Location="myLocation"}

使用 Set-AzResource 来设置 VM 上 $tags 变量中定义的所有标记。

Set-AzResource -ResourceGroupName myResourceGroup -Name myVM -ResourceType "Microsoft.Compute/VirtualMachines" -Tag $tags

使用 Get-AzResource 来显示该资源上的所有标记。

(Get-AzResource -ResourceGroupName myResourceGroup -Name myVM).Tags

输出应如下所示,其中现在包括了新标记:


Key           Value
----          -----
Department    MyDepartment
Application   MyApp1
Created By    MyName
Environment   Production
Location      MyLocation

后续步骤