Step-by-Step: Cleaning up the Cloud with Microsoft Azure and Windows PowerShell
I’ve been delivering lots of hands-on events lately on our Microsoft Azure cloud platform. As part of our events, we step through the process of building complete multi-VM virtual network environments on the Azure platform with VM’s running Windows Server 2012 R2, SQL Server, SharePoint and System Center 2012 R2. By the end of the day, we’ve built lots of VMs, Cloud Services and more!
How do I clean up when I’m done?
A common question at these events is …
When I’m done with my lab VM’s, is there an easy way to clean things up in my Azure subscription without needing to manually delete each VM one-by-one?
Well, good news! There is an easy way to de-provision VMs and Cloud Services in bulk by leveraging just a bit of PowerShell scripting with the Microsoft Azure PowerShell Module.
Proceed with caution!
Note that the PowerShell snippets presented in this article will absolutely de-provision and completely delete selected Cloud Services and their associated VMs and virtual hard disks in your Microsoft Azure subscription. Before running these snippets, make sure that you really do want to delete the selected cloud resources, as once they are deleted, they are gone for good! Use at your own risk. |
Let’s clean up our Cloud!
Follow these steps to de-provision and delete virtual machines and cloud services in bulk within an existing Microsoft Azure subscription.
- Activate a Microsoft Azure subscription ( if you don’t have one yet )
- Provision one or more VM’s and Cloud Services in your Microsoft Azure Subscription.
If you’re looking for a great cloud-based lab guide, check out the Step-by-Step guide for building a multi-VM SharePoint 2013 Farm in the Cloud on Microsoft Azure.
DO IT: Step-by-Step – Build a SharePoint 2013 Server Farm in the Cloud with Microsoft Azure
- Download and install the Microsoft Azure PowerShell Module
You’ll want to install this module into a PowerShell 3.0 or PowerShell 4.0 environment. Note that after installing this module, you may need to restart your PC for the remaining PowerShell snippets to work.
- Launch the Windows PowerShell ISE and run the following PowerShell cmdlets to connect to your Microsoft Azure account.
Set-ExecutionPolicy RemoteSigned
Import-Module Azure
Add-AzureAccount
When prompted to sign-in, enter the username and password that you used when activating your Microsoft Azure subscription.
- In the Windows PowerShell ISE window, run the following PowerShell cmdlet to confirm the name of your Microsoft Azure subscriptions (if you have more than one subscription).
Get-AzureSubscription | Format-Table –Property SubscriptionName
You’ll be specifying the name of your selected subscription in the next step.
- In the Windows PowerShell ISE window, run the following PowerShell cmdlets to select your Microsoft Azure subscription (if you have more than one subscription).
$subscription = “ENTER YOUR SUBSCRIPTION NAME HERE”
Select-AzureSubscription –Default $subscription
- And now for the “clean up” magic … run the following PowerShell code block to delete everything for the selected Cloud Services in your Microsoft Azure subscription.
Get-AzureService |
Select-Object @{"Label"="ServiceName";Expression={$_.Label}} |
Out-GridView -Title "Select VM Deployments to Remove" -PassThru |
ForEach-Object {
Remove-AzureDeployment -ServiceName $_.ServiceName `
-Slot Production -DeleteVHD -Force
Remove-AzureService -ServiceName $_.ServiceName -Force
}
So, let’s take a deeper look at what this last PowerShell snippet actually does …
First, we use the Get-AzureService cmdlet to generate a list of all provisioned Cloud Services within our Microsoft Azure subscription.
This list of provisioned Cloud Services is then passed to the Select-Object cmdlet so that we can pull in just the property for each Cloud Service that we care about: the name of each Cloud Service.
Once we have the name of each provisioned Cloud Service, we pass that list to the Out-GridView cmdlet. Out-GridView provides us with a nice GUI-based selection listbox from which we can easily select just the Cloud Services, and their associated VM's and virtual hard disks, that we wish to deprovision and delete.
The selected Cloud Services are then passed into a ForEach-Object block, where we use two cmdlets to do the actual deprovisioning and deleting of all resources for the selected Cloud Services: Remove-AzureDeployment is used to remove the VMs and their associated virtual hard disks and Remove-AzureService is used to remove the Cloud Services themselves.
Rinse and Repeat …
Now that your Microsoft Azure subscription is cleaned up, you may wish to try out another lab-based scenario with Microsoft Azure. Be sure to check out the resources below for additional step-by-step lab guides for building other cloud scenarios on Microsoft Azure.
What are your cloud scenarios?
Are there particular cloud scenarios in which you have interest? Feel free to leave your comments and feedback below, and we’ll try to write-up as many additional scenarios as possible in the coming weeks.
See you in the Clouds!
- Keith