1

This is a working solution but not very clean:

if [ "$(az vm list -d -o table --query "[?name=='VM_NAME']")" == "$(az vm list -d -o table --query "[?name=='ABSURD_NAME_THAT_CERTAINLY_DOES_NOT_EXIST']")" ]; 
then
    printf "VM DOES NOT EXIST YET"
else
    printf "VM ALREADY EXISTS"
fi

I couldn't figure out, what
"$(az vm list -d -o table --query "[?name=='ABSURD_NAME_THAT_CERTAINLY_DOES_NOT_EXIST']")"
returns which is why I couldn't shorten this solution.
I'm certainly no expert in Bash scripting and I don't know how to convert the result to hexvalues or similar to make it visible.

So I am looking for either a short version of this solution or for another more clean approach. Unfortunately, I couldn't find anything like az vm exists.

Lavair
  • 123

2 Answers2

1

You might use Azure Graph in order to do it.

az graph query -q "Resources | where type =~'Microsoft.Compute/virtualMachines' | limit 1"

You can see some example here.

Resources
| where type =~ 'microsoft.compute/virtualmachines' and name matches regex @'^Contoso(.*)[0-9]+$'
| order by name asc
Taguada
  • 151
1

I found the solution a while ago. The output of the az vm list command if no vm is available is apparently the empty string. But you have to use one equal sign instead of two for the comparison.

if [ "$(az vm list -d -o table --query "[?name=='VM_NAME']")" = "" ];
then
    echo "No VM was found."
else
    echo "VM was found."
fi
Lavair
  • 123