Skip to content

Instantly share code, notes, and snippets.

@itzrahulsoni
Last active October 9, 2017 10:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save itzrahulsoni/d3bb7cd7ba8dc31b2f87507fd4a3d8bd to your computer and use it in GitHub Desktop.
Save itzrahulsoni/d3bb7cd7ba8dc31b2f87507fd4a3d8bd to your computer and use it in GitHub Desktop.
Manage VMs of a specific Resource Group in Azure
#!/bin/bash
AZURE_RESOURCE_GROUP='YOUR_RESOURCE_GROUP_NAME'
if [ $1 == "start" ];
then
VM_NAMES=$(az vm list -g $AZURE_RESOURCE_GROUP --show-details --query "[?powerState=='VM deallocated'].{ name: name }" -o tsv)
for NAME in $VM_NAMES
do
echo "Starting $NAME"
az vm start -n $NAME -g $AZURE_RESOURCE_GROUP --no-wait
done
fi
if [ $1 == "stop" ];
then
VM_NAMES=$(az vm list -g $AZURE_RESOURCE_GROUP --show-details --query "[?powerState=='VM running'].{ name: name }" -o tsv)
for NAME in $VM_NAMES
do
echo "Stopping $NAME"
az vm deallocate -n $NAME -g $AZURE_RESOURCE_GROUP --no-wait
done
fi
if [ $1 == "status" ];
then
echo "Fetching Details..."
az vm list -g $AZURE_RESOURCE_GROUP --show-details
echo "Fetching Summary..."
echo "Power Status of all VMs"
echo "-----------------------"
az vm list -g $AZURE_RESOURCE_GROUP --show-details --query "[].{name: name, status: powerState}" -o table
fi
if [ $1 == "ip" ];
then
az vm list -g $AZURE_RESOURCE_GROUP --show-details --query "[?powerState=='VM running'].{ name:name, ip: publicIps }" -o table
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment