【问题标题】:How to query VMs in Azure powerstate using tags如何使用标签在 Azure powerstate 中查询 VM
【发布时间】:2018-03-30 12:57:39
【问题描述】:

我有一个脚本,它根据分配的标签取消分配订阅中的所有虚拟机 - 下班时间,并在第二天使用 Jenkins 重新启动它们。我希望能够根据状态(Running/Stopped(deallocated) 查询这些虚拟机并将其输出到文件中。

启动命令 - az vm start --ids $(az resource list --tag Restart=${TAG_RESTART} --query "[?type=='Microsoft.Compute/virtualMachines'].id" -o table)

查询命令 - az resource list --tag Restart=yes --query "[].{Name:name,Group:resourceGroup,Location:location}" -o table

此命令返回输出(名称、RG 和位置)。一旦重启脚本完成,我希望它还显示电源状态和可能的操作系统类型。如果还可以将输出导出到电子表格。

【问题讨论】:

  • 试过这个az vm list --ids $(az resource list --tag Restart='yes' --query "[].{Name:name,Group:resourceGroup,Location:location,PowerState:powerState=='running'}" -o table) 得到这个错误az : ERROR: az: error: unrecognized arguments: --ids Name Group Location ------------- ------------- ---------- hazrnv000er01 dvops-prod-rg eastus At line:1 char:1 + az vm list --ids $(az resource list --tag Restart='yes' --query "[].{ ... + CategoryInfo : NotSpecified: (ERROR: az: erro...prod-rg eastus:String) [], RemoteExcepti on + FullyQualifie..

标签: azure virtual-machine azure-cli


【解决方案1】:

您可以使用 az vm show -d --ids 来获取 powershell 状态。

抱歉,我没有 Mac 虚拟机。在 Linux VM 上,我使用以下命令来获取它。

az vm show  -d --ids $(az resource list --tag Restart=shui --query "[?type=='Microsoft.Compute/virtualMachines'].id"|jq -r ".[]") --query 'powerState'

在 Mac 上,也许你可以使用以下命令。

az vm show -d --ids $(az resource list --tag Restart=${TAG_RESTART} --query "[?type=='Microsoft.Compute/virtualMachines'].id" -o table) --query 'powerState'

您可以通过az vm show -h获得帮助

--show-details -d  : Show public ip address, FQDN, and power states. command will run slow.

【讨论】:

  • 非常感谢,这很有帮助
猜你喜欢
  • 1970-01-01
  • 2019-05-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-20
  • 2022-07-08
相关资源
最近更新 更多