Some tasks on azure are easier if we automate them. The Azure Portal provides us the cloud shell, which we can use for this kind of automation.
I was making some experiences with SQL Server Always On, so I created three VMs inside a resouce group. Every time I want to start some experiment I need to start all three VMs and, in the end, stop all three again.
This is not a complex task, but it’s not something we would like to type every time. Using Azure CLI, we have the following instructions available:
az account set: Choose the default subscription. If you have more than a single subscription, you would like
az vm list : List all VM’s and it’s possible to filter by resource group
az vm start/stop: Start or stop the vm
In order to start or stop the VMs, we can combine the CLI statements using powershell. They will be like this:
On these examples we are using the AZ VM LIST to retrieve the list of Ids of the VMs and use this list to start or stop the VMs.
–query extracts only the .id from the JSON object returned by the LIST statement
-o tsv specifies the format, tab separated values, for the Ids
We can prepare the script to receive two parameters, the operation we will execute (start/stop) and the name of the resource group. The first line of the script will be like this:
#blogs #python #azure cli