Share:
Twitter
LinkedIn
Facebook
Reddit
Whatsapp
Follow by Email
Reading Time: 3 minutes

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

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

Add-AzureRmAccount

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"

To get a list of available functions type:

Get-Command -Module AzureRm.AvailabilitySetManagement

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

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"

[su_note note_color=”#ff0000″ text_color=”#000000″ radius=”3″ class=””]
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.
[/su_note]
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"

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

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

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

Jörg Schoppmann · February 14, 2020 at 9:43 am

Just found your post here and wondering if there is any data carried over to the new VM… I have an existing CA Server in Azure (not used yet but configuration is done) and would like it to be in an availability set but not set up everything from scratch .Is this something I can accomplish with your script?

Cheers from Germany

Oleg · March 22, 2020 at 5:30 pm

Good, working solution with good description.
It’ll be good to add info, that for PShell 5.1+ AzureRM-commandlets can’t be used with az-commandlets in parallel (they override each other).
So, to make this script work I’ve installed AzureRM modules with next command:
“Install-Module -Name AzureRM -RequiredVersion 5.1.1 -AllowClobber”. Restarted PS. And then went step-by-step with your instructions.
Thanks a lot!

Freir Man · October 8, 2022 at 1:58 am

When I tried to run this script, it just deleted all the resources related to VM (VM instance, disk and nik) So I need to redeploy all the things …
New-AzureRmResourceGroupDeployment : 3:47:50 AM – Resource Microsoft.Compute/virtualMachines ‘AZ-UKS-DAP-P02’ failed with message ‘{
“error”: {
“code”: “NotFound”,
“message”: “Disk AZ-UKS-DAP-P02_OsDisk_1_2ca17bd584a541998d5f44f9f13642e8 is not found.”,
“target”: “/subscriptions/d6d1eeeb-1796-4e08-b4d1-f2b2cacb1486/resourceGroups/FG-UKS-HUB-DuoAuthProxy-RG/providers/Microsoft.Compute/disks/AZ-UKS-DAP-P02_OsDi
sk_1_2ca17bd584a541998d5f44f9f13642e8”
}
New-AzureRmResourceGroupDeployment : 3:47:50 AM – Template output evaluation skipped: at least one resource deployment operation failed. Please list deployment
operations for details. Please see https://aka.ms/DeployOperations for usage details.
At C:\Users\XXX\Documents\WindowsPowerShell\Modules\AzureRm.AvailabilitySetManagement\1.0.0.1\AzureRm.AvailabilitySet.CoreHelper.psm1:239 char:8
+ … New-AzureRmResourceGroupDeployment -Name “SettingUpAvaila …

Leave a Reply

Avatar placeholder

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