One big limitation you may encounter when dealing with Microsoft Azure is the ability to change an Azure Virtual Machines Availability Set configuration. This includes adding an existing VM to an Availability Set, move a VM from one Availability Set to another, and removing a VM from an Availability Set. In this article, I will go through the process of doing this using a PowerShell script created by Paulo Marques.

In fact you can vote for this feature to be added via this link.

Prerequisites

You will need to have the latest PowerShell module installed.

Now we can import the PowerShell module from the PowerShell gallery using the following command.

Install-Module AzureRm.AvailabilitySetManagement
view raw MoveVMAVASet.ps1 hosted with ❤ by GitHub

Now you have to connect up to your Azure subscription. Use the following command to connect up.

Add-AzureRmAccount
view raw MoveVMAVASet.ps1 hosted with ❤ by GitHub

Now its time to select the Azure subscription you want to work with. This can be done using the following command.

Select-AzureRmSubscription -SubscriptionID "your subscription ID"
view raw MoveVMAVASet.ps1 hosted with ❤ by GitHub

To get a list of available functions type:

Get-Command -Module AzureRm.AvailabilitySetManagement
view raw MoveVMAVASet.ps1 hosted with ❤ by GitHub

If you do not have an Availability Set already create one now. You can create one using the following PowerShell. Just change the settings to match your set up.

New-AzureRmAvailabilitySet -Location "East US 2" -Name "myAvailabilitySet" -ResourceGroupName "PixelRobots" -Sku aligned -PlatformFaultDomainCount 2 -PlatformUpdateDomainCount 2
view raw MoveVMAVASet.ps1 hosted with ❤ by GitHub

You are now ready to move your VM to an Availability Set.

Add A Virtual Machine To An Availability Set

You can use the following code to do that. Please note that your VM will be unavailable during the move.

Add-AzureRmAvSetVmToAvailabilitySet -ResourceGroupName "PixelRobots" -VMName "VM1" -OsType windows -AvailabilitySet "myAvailabilitySet"
view raw MoveVMAVASet.ps1 hosted with ❤ by GitHub

One thing to note is that any existing VM Extension will not be exported, therefore you need to re-enable those if they were previously there.
You can now check the Availability Set to see if your VM has been moved by using the following command

Get-AzureRmAvailabilitySet -ResourceGroupName "PixelRobots" -Name "myAvailabilitySet"
view raw MoveVMAVASet.ps1 hosted with ❤ by GitHub

Remove A Virtual Machine From An Availability Set

To remove a virtual machine from an availability set use the following command. Please note that your VM will be unavailable during the move.

Remove-AzureRmAvSetVmFromAvailabilitySet -ResourceGroupName "PixelRobots" -VMName vm1 -OsType windows
view raw MoveVMAVASet.ps1 hosted with ❤ by GitHub

That’s it. You can now easily move and remove an existing Azure Virtual Machines into an Availability Set. I hope you found this article helpful. If you have any questions please drop a message below.


Pixel Robots.

I’m Richard Hooper aka Pixel Robots. I started this blog in 2016 for a couple reasons. The first reason was basically just a place for me to store my step by step guides, troubleshooting guides and just plain ideas about being a sysadmin. The second reason was to share what I have learned and found out with other people like me. Hopefully, you can find something useful on the site.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

I agree

This website stores some user agent data. These data are used to provide a more personalized experience and to track your whereabouts around our website in compliance with the European General Data Protection Regulation. If you decide to opt-out of any future tracking, a cookie will be set up in your browser to remember this choice for one year. I Agree, Deny
640