Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# A valid password requires a length of 12 and at least one upper/lower/digit
pattern="(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).*"
# 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]).*"
do
random12=$(head /dev/urandom | tr -dc a-zA-Z0-9 | fold -w 12 | head -n 1)
done
region="eastus"
group="rg-disk-test-$random12"
vm1name="vm-disk-test-$random12"
# 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 "{ os:storageProfile.osDisk.managedDisk.id }")
# 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
You can’t perform that action at this time.