How to Assign an availability set to a VM in Azure
Assigning Availability Set At The Time Of Creation Of VM
At the point of creation of a VM in a cloud, you’ll get an option to put that VM in a specific Availability Set. After the creation of VM you can’t change its availability set or to put it in a new one through GUI..
Change The Availability Set Using PowerShell
A VM can only be added to an availability set when it is created. In order to change the availability set, you need to delete and recreate the virtual machine.
Capture the following key details from the VM to be modified.
Name of the VM
$vm = Get-AzureRmVM -ResourceGroupName -Name $vm.Name
VM Size
$vm.HardwareProfile.VmSize
Network primary network interface and optional network interfaces if they exist on the VM
$vm.NetworkProfile.NetworkInterfaces[0].Id
OS Disk Profile
$vm.StorageProfile.OsDisk.OsType $vm.StorageProfile.OsDisk.Name $vm.StorageProfile.OsDisk.Vhd.Uri
Disk profiles for each data disk
$vm.StorageProfile.DataDisks[].Lun $vm.StorageProfile.DataDisks[].Vhd.Uri
VM extensions installed
$vm.Extensions
Delete the VM without deleting any of the disks or the network interfaces.
Remove-AzureRmVM -ResourceGroupName -Name
Create the availability set if it does not already exist
New-AzureRmAvailabilitySet -ResourceGroupName -Name -Location ""
Recreate the VM using the new availability set
$vm2 = New-AzureRmVMConfig -VMName -VMSize -AvailabilitySetId Set-AzureRmVMOSDisk -CreateOption "Attach" -VM -VhdUri -Name [-Windows | -Linux] Add-AzureRmVMNetworkInterface -VM -Id New-AzureRmVM -ResourceGroupName -Location -VM
Updated on: 31/01/2023
Thank you!