如何在电源状态为 运行 且特定标记为空的情况下获取 Azure 中的 VM 的 ID?

How to get the id of a VM in azure where power state is running and a specific tag is null?

因此,我正在尝试获取所有订阅和区域中所有虚拟机的 ID,其中特定标记为空。为此,我使用以下命令

az vm list -d --query '[?!not_null(tags.run)]|[].id'

请注意:我只想在标签不存在时获取 ID

请注意,我需要使用单引号来覆盖查询,因为我使用的是 '!'运算符来反转 not_null() 函数。如果我使用双引号 bash 将抛出 event not found 错误。

所以现在问题出现了,当我还想添加一个条件来检查 VM 的当前状态和 return id 仅当它是 运行 并且标签不存在时。

az vm list -d --query '[?!not_null(tags.run)] | [?powerState=="VM running"].id'

这里我必须用双引号将 VM running 括起来,这给了我一个空输出,因为字符串没有被匹配,因为查询需要像这样的单引号 -

"[?powerState=='VM running'].id"

有人可以帮我解决这个问题吗?

VM 运行 字符串使用 raw string literals。您只需要用反引号和双引号将字符串括起来。

az vm list -d  --query '[?!not_null(tags.run)]|[?powerState==`"VM running"`].id'