Reading Time: 3 minutes
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.

12 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.

Mark · December 3, 2018 at 5:57 pm

When I run the script to add the VM back into the availability group I get the error below. Any ideas how to work around this. I am stuck with trying to add a restored VM back into a SQL cluster or the horrible thought of a complete rebuild.

Error: Code=InvalidTemplate; Message=Deployment template validation failed: ‘The number of template input parameters limit exceeded. Limit: ‘256’ and actual: ‘388’.

At C:\Program Files\WindowsPowerShell\Modules\AzureRm.AvailabilitySetManagement\1.0.0.1\AzureRm.AvailabilitySet.CoreHelper.psm1:239 char:8
+ … New-AzureRmResourceGroupDeployment -Name “SettingUpAvaila …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [New-AzureRmResourceGroupDeployment], Exception
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.NewAzureResourceGroupDeploymentCmdlet

New-AzureRmResourceGroupDeployment : The deployment validation failed
At C:\Program Files\WindowsPowerShell\Modules\AzureRm.AvailabilitySetManagement\1.0.0.1\AzureRm.AvailabilitySet.CoreHelper.psm1:239 char:8
+ … New-AzureRmResourceGroupDeployment -Name “SettingUpAvaila …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [New-AzureRmResourceGroupDeployment], InvalidOperationException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.NewAzureResourceGroupDeploymentCmdlet

    Pixel Robots. · December 3, 2018 at 6:13 pm

    Hi mark, I am currently away on holiday. But looking at the error I think maybe the new resource group name needs looking at. Try something smaller and make sure it matches the requirements of a resource group name.

    Hope that helps.

netlabindia · February 19, 2019 at 2:34 pm

I get the error below while adding existing VM to AV set

PS C:\windows\system32> Add-AzureRmAvSetVmToAvailabilitySet -ResourceGroupName “CIN-INTDEV-UTX-WL-RG1” -VMName “CENTOS7” -OsType Linux -AvailabilitySet “testAV”
VERBOSE: Original resource group ARM template file name: C:\Program Files (x86)\WindowsPowerShell\Modules\AzureRm.AvailabilitySetManag
ement\1.0.0.1\OriginalTemplate-2019-02-19_070014.json
VERBOSE: New resource group ARM template file name: C:\Program Files (x86)\WindowsPowerShell\Modules\AzureRm.AvailabilitySetManagement
\1.0.0.1\NewTemplate-2019-02-19_070014.json
VERBOSE: Getting the existing AvailabilitySet
VERBOSE: Exporting resource group
WARNING: ExportTemplateCompletedWithErrors : Export template operation completed with errors. Some resources were not exported. Please
see details for more information.
WARNING: ExportTemplateProviderError : Could not get resources of the type ‘Microsoft.OperationalInsights/workspaces/dataSources’. Res
ources of this type will not be exported.
WARNING: ExportTemplateProviderError : Could not get resources of the type ‘Microsoft.Storage/storageAccounts/managementPolicies’. Res
ources of this type will not be exported.
WARNING: ExportTemplateProviderError : Could not get resources of the type ‘Microsoft.Storage/storageAccounts/blobServices’. Resources
of this type will not be exported.
WARNING: ExportVirtualMachineExtensionNotSupported : Virtual machine extension ‘Microsoft:2EEnterpriseCloud:2EMonitoring-OmsAgentForLi
nux’ is not supported for template export.
WARNING: ExportVirtualMachineExtensionNotSupported : Virtual machine extension ‘Microsoft:2EAzure:2EDiagnostics-LinuxDiagnostic’ is no
t supported for template export.

VERBOSE: Generating the new JSON Template to be executed to import the VMs back.
Add-AzureRmAvSetVmToAvailabilitySet : An error ocurred: Resouces section of template is empty after transformations, aborting
operation.
At line:1 char:1
+ Add-AzureRmAvSetVmToAvailabilitySet -ResourceGroupName “CIN-INTDEV-UT …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Add-AzureRmAvSetVmToAvailabilitySet

Jeremiah Allen · April 24, 2019 at 1:09 am

Is this process works for Managed disks?

    Pixel Robots. · April 24, 2019 at 9:11 am

    I have only ever used it with managed disks so yes.

palms · April 30, 2019 at 11:55 am

Hi, are you moving the VM to an AV Set, or deleting the original and re-creating it in the Av Set?

    Pixel Robots. · April 30, 2019 at 12:21 pm

    This script deletes the original and creates a new one in the AV set.

palms · April 30, 2019 at 12:51 pm

Appreciate the quick response, thank you. If this is the case then, if teh original server is being backed up or part of ASR or anything else, does all that need to put back because this is essentially a new computer with I assume a new SID?

    Pixel Robots. · April 30, 2019 at 12:53 pm

    I don’t believe it will have a new SID, but will have a new Azure resource ID so will probably need to be added into ASR again.

Leave a Reply

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

*

I agree