如何使用标签查询 Azure powerstate 中的虚拟机

How to query VMs in Azure powerstate using tags

我有一个脚本可以根据分配的标签取消分配订阅中的所有 VM - 下班时间并在第二天使用 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

此命令returns 输出(名称、RG 和位置)。我希望它还显示 powerstate,并可能在重启脚本完成后显示 OS 类型。如果还可以将输出导出到电子表格。

您可以使用 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.