Last active
April 28, 2025 22:11
-
-
Save caseywatson/76555af8690eb9e4dd065c676461b065 to your computer and use it in GitHub Desktop.
Use this script to shut down all virtual machines in your Azure subscription. Careful with this one!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Careful with this one! | |
# It shuts down all virtual machines in the given subscription. | |
# Run it like this: | |
# `shut_em_down.sh1 "217991d5-5d31-4f41-9455-b1f871e610b6"` # Example subscription ID | |
SUBSCRIPTION_ID="$1" | |
az account set --subscription $SUBSCRIPTION_ID | |
# Get a list of all virtual machines in the subscription | |
VM_LIST=$(az vm list --query "[].{name:name, resourceGroup:resourceGroup}" -o tsv) | |
# Loop through each VM and shut it down | |
echo "Shutting down all virtual machines in subscription $SUBSCRIPTION_ID..." | |
while IFS=$'\t' read -r VM_NAME RESOURCE_GROUP; do | |
echo "Stopping VM: $VM_NAME in resource group: $RESOURCE_GROUP" | |
az vm deallocate --name "$VM_NAME" --resource-group "$RESOURCE_GROUP" --no-wait | |
done <<< "$VM_LIST" | |
echo "Shutdown commands issued for all virtual machines." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment