簡體   English   中英

通過在 powershell 中使用標簽進行過濾來獲取 Azure VM 詳細信息

[英]Get Azure VM details by filtering using tags in powershell

我想使用Get-AzVm命令來獲取具有特定標簽的 VM 列表。

我試過Get-AzVM | Where-Object {$_.Tags['Resource'] -eq "test"} Get-AzVM | Where-Object {$_.Tags['Resource'] -eq "test"}

仍然沒有返回帶有標簽“資源:測試”的 VMS

get-azvm的輸出不會為 VM 生成標簽。 但是, Get-AzVM -ResourceGroupName ResourceGroupName -Name VMName可以做到這一點。

因此,您需要遍歷 VM,將 VM 標簽存儲在哈希表中,然后通過 hastable 枚舉以檢查您想要的帶有值的標簽是否存在。 這是代碼 -

$VMs = get-azvm
foreach ($VM in $VMs)
{
    [Hashtable]$VMTag = (Get-AzVM -ResourceGroupName $VM.ResourceGroupName -Name  $VM.Name).Tags
    foreach ($h in $VMTag.GetEnumerator()) {
    if (($h.Name -eq "Resource") -and ($h.value -eq "test"))
        {
            Write-host "VM with tags Resource:test are" $VM.Name
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM