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

Time:10-22

So I am trying to get the ID of all VMs across all subscriptions and regions, where a specific tag is null. For this I am using the following command

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

Please note: I want to get the ids only if the tag doesn't exist

Here notice I need to use single quotes to cover the query as I am using the '!' operator to inverse the not_null() function. If I were to use double quotes bash will throw an event not found error.

So now the problem arises when I also want to add a condition to check the current state of the VM and return id only if it is running and tag doesn't exist.

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

Here I have to wrap VM running in double quotes and this gives me an empty output as the string is not being matched because the query expects single quotes like so -

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

Could someone help me with a workaround for this?

CodePudding user response:

Use raw string literals for VM running string. You just have to surround your string with a back tick and a double quote.

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