如何使用 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-AzResource
和 Tags
屬性,將 VM 的所有目前標籤指派給 $tags
變數。
$tags = (Get-AzResource -ResourceGroupName myResourceGroup -Name myVM).Tags
若要查看目前的標籤,請輸入 變數。
$tags
以下是輸出看起來可能的樣子:
Key Value
---- -----
Department MyDepartment
Application MyApp1
Created By MyName
Environment Production
在下列範例中,我們會使用 值 myLocation
新增名為 Location
的標記。 使用 +=
將新的索引鍵/值組附加至 $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
下一步
- 如需深入了解如何標記您的 Azure 資源,請參閱 Azure Resource Manager 概觀與使用標記來組織您的 Azure 資源。
- 若要了解標籤如何協助您管理 Azure 資源的使用,請參閱了解 Azure 帳單。