![](/img/trans.png)
[英]Azure powershell command to get the reserved IP details for a VM in a subscription
[英]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.