Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
# A valid password requires a length of 12 and at least one upper/lower/digit
# Generate a random string of 12 characters to make the names unique and to use as a password
while ! echo $random12 | grep -P "(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*"
random12=$(head /dev/urandom | tr -dc a-zA-Z0-9 | fold -w 12 | head -n 1)
# Create a new resource group for this test
az group create -n $group -l $region
# Don't do this in real life, where the password equals part of the VM name
# Let the command create a new VNET, we're deleting it shortly anyway
az vm create -g $group -l $region -n $vm1name --admin-password $random12 \
--admin-username azureuser --authentication-type password \
--image UbuntuLTS --public-ip-address ""
diskId=$(az vm show -g $group -n $vm1name -o tsv --query "{ }")
# Create a snapshot
az snapshot create -g $group -n "$vm1name-ss" --source $diskId
# Delete the VM
az vm delete -g $group -n $vm1name --yes
# Delete the disk
az disk delete --id $diskId --yes
# Find the snapshot (yes, it's still there)
az snapshot show -g $group -n "$vm1name-ss"
# Clean up: delete the whole RG
az group delete -n $group --yes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment