Share:
Twitter
LinkedIn
Facebook
Google+
Reddit
Whatsapp
Follow by Email

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.

Share:
Twitter
LinkedIn
Facebook
Google+
Reddit
Whatsapp
Follow by Email

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.

3 Comments

MrDave · September 11, 2018 at 5:46 am

Hi,

I’m getting the following error when trying to action this article. Have you tried to run this recently?

Thanks!

What if: Performing the operation “Export” on target “ANZ”.
Add-AzureRmAvSetVmToAvailabilitySet : An error ocurred: Cannot find path ‘C:\Program
Files\WindowsPowerShell\Modules\AzureRm.AvailabilitySetManagement\1.0.0.1\OriginalTemplate-2018-09-06_023253.json’ because it does not exist.
At line:1 char:1
+ Add-AzureRmAvSetVmToAvailabilitySet -ResourceGroupName $RGName -VMNam …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Add-AzureRmAvSetVmToAvailabilitySet

    Pixel Robots. · September 11, 2018 at 5:03 pm

    I have not used it in a while. I will have a little test and let you know.

      Pixel Robots. · September 13, 2018 at 8:44 am

      Hello, I have just tried it and its working fine for me. Just make sure you are running PowerShell as an Administrator and try reimporting the module using this command “Install-Module AzureRm.AvailabilitySetManagement”

      Let me know how you get on.

Leave a Reply

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

*

I agree