Before I worked on server migrations to Azure, I had only worked with Azure for a SQL Saturday session, and I was only using Azure as a host to the virtual machines (VMs) that I used to demonstrate PowerShell scripts running on a windows server that also accessed active directory. My only exposure to this technology was limited and focused on what I could do with the VM – not with how the VM was created or how I could use PowerShell to deploy a VM.
That professional experience with Azure finally came, and I got to work on PowerShell scripts that could be used to create VMs in Azure. With a script you have something that is consistent and less prone to human error, but even with that sometimes something is set up wrong that can’t be changed once the VM has been provisioned. Alternatively, maybe you are only using Azure for the testing of an application of project, you are done and the VM is no longer needed. You could simply decommission the VM, but if there is no need for the VM to still exist you probably want to delete it and all the parts associated with it.
The first one of these happened to me – there were three VMs where the configuration wasn’t right and it wasn’t something that could be changed from the portal. The VMs would have to be deleted and re-provisioned. I modified the creation script to have the right configuration for these VMs and got those created so work could continue. At this point I started to work on a script that would remove the VMs base on the name and resource group, including removal of all the parts that were tied to the VMs, such as the virtual network and the storage account.
Fast forward to today – I am working on my session for PASS Summit 2017. Instead of creating every part separately with the PowerShell cmdlets, PowerShell is used to apply a template that contains all the parameters and variables for a VM. If you need to make a change it is made to the JSON template, not to the PowerShell. Since I am testing, I am creating VMs and then I want to blow them away right after. I attempted to use the PowerShell script I had used before to remove VMs but that failed – enough time had passed that the cmdlets have been updated and what I ran before was no longer valid. I tried to revise the existing script and then ran across the OMG-I-can’t-believe-I-didn’t-do-this-before answer – just delete the resource group.
Remove-AzureRmResourceGroup -Name 'rgsqlkitten' -Verbose -Force
If you have contained all parts of the VM in the same resource group, and there is nothing else in the resource group other than the VM, cleanup is this simple. From what I recall about the server migration, there were multiple VMs associated with the resource group. If the resource group had only contained the VMs I wanted to blow away, I could have potentially run this for that clean-up operation. In this case, I was also removing disks from each of the VMs ahead of removing the VMs and had to run a script for that first. Looking back, if everything had been in one resource group, and had there been nothing else in there, I could have run this one line and been done.
For now, this solution works for me, but I know a more specific cleanup script may be more useful – one that will remove a VM and all of its parts based on the VM and the resource group. This will have to happen when I am done with my other testing, but in the meantime, this one line of code allows me to create and blow away VMs while using my personal Azure account…and not run up a ton of charges. Let me know if you find this useful or if you have any suggestions here – would love to have feedback!