- For more on the 31 Days of Server in the Cloud Series – see here: http://mythoughtsonit.com/2013/01/31-days-of-servers-in-the-cloud-windows-azure-iaas-and-you/
- Before you are able to execute these PowerShell commands against your Azure environment you need to configure your workstation. You can follow these instructions here: http://mythoughtsonit.com/2013/01/31-days-of-servers-in-the-cloud-setting-up-management/
- To create Servers with PowerShell see this post here:
- If you want to setup a Free Azure subscription to work against you can get a free 90 trial here: http://www.microsoft.com/click/services/Redirect2.ashx?CR_CC=200120070
Deleting VHDs in Azure
In Azure you must delete your Virtual Machine so that Azure stops charging you. Just turning off your server is not enough to stop the charging. This is because the CPU is reserved for your Server. To stop the charging you must delete the VM.
When you delete a Virtual Machine in Azure you delete the reservation for the CPU but that doesn’t delete your data. The data including the OS volume still exist in Azure Storage in VHD files. The configuration of your server also still exists in a service profile. This allows you to bring your server back by creating a “new” server and choosing the old server name. It will bring back your old server.
If you want to get rid of the server entirely you need to delete the VHD file. To delete the VHD file you just need to click on the Disks Menu and then delete your disk. Lastly choose to delete the VHD file or it will stay in your Azure Storage.
Sometimes Azure is unable to delete the VHD files because there is still a lease on the file. That is a problem that happens occasionally and what we need to do is break the lease. Unfortunately this currently can not be done from the management portal. Luckily it can be done via our good friend PowerShell.
How to use PowerShell script to break the Azure lease:
- Setup the PowerShell management environment. You can follow my steps here:
- Download the script BreakLease.ps1 from the link below:
I extracted the breaklease.ps1 to C:\temp
- Remember to run Powershell as Administrator. Make sure the execution policy allows you to run scripts. You can check with the Get-ExecutionPolicy command. You can set the execution policy to allow you to run scripts locally with this command: Set-ExecutionPolicy Unrestricted
(Prompt must have admin privileges)
- Run BreakLease.ps1 with the URL to the VHD in order to break the lease. The script obtains the necessary storage account information, checks that the blob is not currently registered as a disk or as an image, then proceeds to break the current lease (if any). This URL is found under Storage, <storage name>, Containers, vhds. You find the URL in the Azure portal as below:
Hope that helps. If you use this to fix your issues – leave me a message and let me know it was useful! –Brian