-
Notifications
You must be signed in to change notification settings - Fork 0
/
delvm.sh
executable file
·66 lines (52 loc) · 1.63 KB
/
delvm.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/bin/bash
function usage {
echo "Deletes a VM with all its associated resouces, except Data disks."
echo "Usage:"
echo " delvm.sh <virtual machine name> <resource group>"
echo ""
echo "Arguments"
echo " <Virtual machine name> The name of the Virtual machine to delete."
echo " <resource group> The name of the resource group to search."
echo ""
echo "Example:"
echo " delvm.sh myvm01 mygroup"
exit
}
# Check to see if we have two arguments at least
if [[ $1 = "" ]] | [[ $2 = "" ]]; then
usage
fi
echo "Gathering information for VM" $1 "in Resource Group" $2
# Get VM ID
vmID=$(az vm show -n $1 -g $2 --query "id" -o tsv)
# Did we find the VM?
if [[ $vmID = "" ]]; then
echo Could not find VM $1
exit
fi
# Get OS Disk
echo "Seeking Disk."
osDisk=$(az vm show -n $1 -g $2 --query "storageProfile.osDisk.name" -o tsv)
# Get a list of public UP addresses
echo "Sniffing IPs.."
ipArray=$(az vm list-ip-addresses -n $1 -g $2 --query "[].virtualMachine.network.publicIpAddresses[].id" -o tsv)
# Get a list of NICs
echo "Getting NICs.."
nicArray=$(az vm nic list --vm-name $1 -g $2 --query "[].id" -o tsv)
# Get a list of NSGs
nsgQry="[?virtualMachine.id=='"
nsgQry+=$vmID
nsgQry+="'].networkSecurityGroup[].id"
echo "Discovering NSGs.."
nsgArray=$(az network nic list -g $2 --query $nsgQry -o tsv)
echo Deleting VM $1
az vm delete -n $1 -g $2 --yes
echo Deleting Disk ID $osDisk
az disk delete -n $osDisk -g $2 --yes
echo Deleting Network cards $nicArray
az network nic delete --ids $nicArray
echo Deleting IPs $ipArray
az network public-ip delete --ids $ipArray
echo Deleting NSG $nsgArray
az network nsg delete --ids $nsgArray
echo Done with $1.