Add or change an ARM Virtual Machine’s Availability Set

Hi all,

One of the limitations we may encounter when dealing with Azure ARM Virtual Machines is the ability to manipulate the VM’s availability Set configuration after the VM deployment. In fact :

  • You can’t change the VM’s Availability Set once the VM is created
  • You can’t add an Azure VM to an Availability Set once the VM is created
  • You can’t remove a VM from an Availability Set

This is a big limitation since we may need such feature, in different cases:

  • We need to add an existing VM to a highly available pool
  • We want to change the Availability Set name
  • We messed up with the Availability Set  naming

I think this feature will come in the future, but the far or the near future, I have no idea. Maybe by the end of the year (Q4 !)

Till that time, I wrote this Powershell script, which will enable you to manage an ARM VM’s availability Set


  • Add a VM to an Availability Set
  • Change a VM’s Availability Set
  • Remove a VM from an Availability Set

How it works ?

The script will:

  1. Get the VM configuration
  2. Save it to a local location (If something goes wrong, we can recreate the VM)
  3. Remove the VM (Only the configuration, all related objects are kept)
  4. Create a new  VM configuration with the AS config (Add AS, Remove AS, Change AS)
  5. Recreate the VM

How to use it ?

1- Download the script and save it to local location

2- Run it and provide the requested parameters


2- ./Set-ArmVmAvailabilitySet.ps1 –VmName ‘The VM Name’ –ResourceGroup ‘Resource Group’ –AvailabilitySetName ‘As Name’ –SubscriptionName  ‘The Subscription name’


To remove a VM from an AvailabilitySet:

./Set-ArmVmAvailabilitySet.ps1 –VmName ‘The VM Name’ –ResourceGroup ‘Resource Group’ –AvailabilitySetName 0 –SubscriptionName  ‘The Subscription name’



Download Link

Version 1.01 :

Version 1.0 : (Retired)


26 thoughts on “Add or change an ARM Virtual Machine’s Availability Set

  1. Hey, Great script! Works wonders for us.

    Is there a way to rebuild the machine with multiple NICs as well? Since we have quite a few machines with 1 NIC only, we need to make many of them multi-NIC and don’t want to start from scratch. Any thoughts?

    • Hi,
      I will post this night a script, which one of the features is to pass from 1 NIC to multiple NICs. But it works differently for this script, becasue it’s intended to move VMs from one location to another, but it can be used to transform the VM to multiple NICs VM.
      However, stay tuned, i will publish a script which add NICs to a VM just like this one

  2. So I was running into an authentication null issue while trying to run this script for a customer that is federated. The fix was to set it up to run from a machine logged into their domain with a federated account with creds in Azure and it worked. So if you are like me and have to work in several different customer’s Azure environment this script would not prompt me for creds when I ran it from machines not logged into customer domain with Azure admin account.

  3. Hi Samir, great script. It worked a treat. Only issue is that it doesnt seem to retain the custom DNS server settings. Not a big issue, just thought you’d like to know.


  4. Hi Samir, Great. this helps me resolve my problem. but the problem I got is after creating the VM from the script it seems that VM agent extension not loaded. so I don’t see VM CPU matrix in Azure Portal.

    in another thread it was saying exporting the VM template from this script does not include VM agent extensions export.

    Could you let me know how do I enable default VM Usage matrix in azure portal. I’m using ARM mode.


  5. Hi, nice article but I was tried this tutorial and getting nothing. Is there support for Classic VM? Because I use Classic VM in Azure. I read some articles, there is a different between Classic VM and Modern VM (ARM). Any command about that? Thanks for your respond 🙂

  6. Hello, Samir!

    Thank you for the ps1 script. It worked on 3 of 4 of my servers. The first attempt seemed to fail on the VM creation step (Step 2). I could see the VHD in the blob but the VM never rebuilt. Just thought you’d like to know.

    The real question I have is with the ‘Computer Name’ when I am logged into the Portal. When I navigate to my Resource Group and then click on the VM, the Overview has a field called ‘Computer Name’. After running your script on an existing VM, that field is no longer populated. It is just a single dash ‘ – ‘. Any idea why this may be? In Windows, the name is correct. I tried resizing the server to get it to reload (and of course I tried rebooting) but this field remains blank.

      • Also, sorry. The script failed, now the VM’s are deleted from Azure. With the .json file that was created, how can I re-deploy the VM’s back to Azure (in the Availability Set). The .json looks much different and is missing resources, parameters, etc.

        Thanks a lot

  7. *Update*

    May have found issue why it failed. The new “managed disk” feature. VM’s were not on managed disks. The new Availability Set was, however (defaulted to this now when you create one). Lovely.

  8. Hi, after running the script with no error the VM never appeared? just seems to have deleted and not recreated?

  9. I keep getting the following errors.

    Environment : AzureCloud
    Account :
    TenantId : d67e5453-732f-4adc-94a4-4888f2d97d5d
    SubscriptionId : 1a6007e7-7eee-4767-87c7-9ebb138bc2e0
    SubscriptionName : Azure Shared Infrastructure Services
    CurrentStorageAccount :

    Validating the Vm Existence …
    Validating the As Existence …
    Validation of [West-IIS-1] and [Humza-West-IIS-Avail] : Failed
    Validation of [West-IIS-1] : False
    Validation of [Humza-West-IIS-Avail] : False

  10. The virtual server disappered, but was not recreated like the script said it would be. Any ideas? The resources for the server are still there, but not the virtual server.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s