【问题标题】:How do I use Powershell to return VM Name, TagName, TagValue in Azure?如何使用 Powershell 在 Azure 中返回 VM Name、TagName、TagValue?
【发布时间】:2022-01-06 20:51:08
【问题描述】:

我还不太擅长在 PowerShell 中解析和显示哈希表中的值。

我想使用命令来显示虚拟机名称、特定标签的名称以及该虚拟机的标签值。

类似于其中之一(即使我知道这些命令不起作用,但这是我正在尝试做的):

Get-AzResource -Name SYSTEM* -ResourceType "Microsoft.Compute/virtualMachines" -TagName BuiltBy | Select Name,TagName,TagValue

Name      TagName   TagValue
----      -------   --------
SYSTEM1   BuiltBy   Frank
SYSTEM2   BuiltBy   Frank
SYSTEM3   BuiltBy   Betty
SYSTEM4   BuiltBy   Frank

Get-AzResource -Name SYSTEM* -ResourceType "Microsoft.Compute/virtualMachines" -TagName BuiltBy | Where {$.TagValue -eq 'Betty'} | Select Name,TagName,TagValue

Name      TagName   TagValue
----      -------   --------
SYSTEM3   BuiltBy   Betty

Get-AzResource -Name SYSTEM* -ResourceType "Microsoft.Compute/virtualMachines" -TagName BuiltBy | Where {$.TagValue -ne 'Frank'} | Select Name,TagName,TagValue

Name      TagName   TagValue
----      -------   --------
SYSTEM3   BuiltBy   Betty

【问题讨论】:

    标签: azure-powershell


    【解决方案1】:

    以下是用于查找 VM 名称、标记名称和标记值的 PowerShell 脚本

    $vmlist=Get-AzResource -ResourceType 'Microsoft.Compute/virtualmachines'  
    foreach( $item in $vmlist){ if($item.Tags.Keys -eq 'CreatedDate' -and $item.Name -eq 'shbhost'){  
    
      
    
    get-azvm -Name $item.Name |select -Property Name,Tags
    
      
    
    }}
    

    输出

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多