Bash Shell Script to Start/Stop/Deallocate/Show Multiple Azure Virtual Machines
This article shows shell script to Start/Stop/Deallocate/Show Azure Virtual Machines.
Note: To Start/Stop/Deallocate/Show any Virtual Machine in Azure, the user need to have at least Contributor role on that Azure Subscription.
Step 1: Create a new .rc file with Azure Subscription Name, this is the file to mention all the multiple virtual machines names.
Example: <subscription_name>.rc
Mention virtual names inside “<subscription_name>.rc” file as like below,
# Enter the Virtual Machines Names
vm_names=(“vm01” “vm02” "vm03")
Step 2: Create a shell script file (vm_action.sh) for setting the Azure Subscription and getting the Virtual Machines from .rc file and performing the action (start/stop/deallocate/show).
Below Shell Script Code shows,
- First, need to set Azure Subscription using az CLI command from the list of subscriptions with our ID.
- Create AZ_ACCOUNT variable to get the Azure Subscription name for <subscription_name>.rc file.
- Then declare the . AZ_ACCOUNT.rc file for getting the Virtual Machines Names.
- Now have to know the Resource Group name to start/stop/deallocate or show the Virtual Machines.
- Create new file VM_LIST_FILE for all the values inside the “/tmp/$AZ_ACCOUNT.vm.list”, this is used to copy all the details for the Virtual Machines using az CLI command: az vm list — output table > “$VM_LIST_FILE”.
- Now select action needs to be performed on each Individual Virtual machine.
To Download full shell script code,
Click here: raw code
Thank you