Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Applies to: ✔️ Linux VMs ✔️ Windows VMs
The following steps describe how to change the availability set of a virtual machine (VM) by using Azure PowerShell. You can add a VM to an availability set only when you're creating the VM. To change the availability set, you need to delete and then re-create the VM.
This article was last tested on February 12, 2019 by using the Az PowerShell module version 1.2.0.
Warning
This is just an example. In some cases, you'll need to update it for your specific deployment.
Make sure the disks are set to detach
as the delete option. If they're set to delete
, update the VMs before you delete the VMs.
If your VM is attached to a load balancer, you need to update the script to handle that case.
Some extensions might also need to be reinstalled after you finish this process.
If your VM uses hybrid benefits, you need to update the example to enable hybrid benefits on the new VM.
Prerequisites
Change the availability set
The following script provides an example of gathering the required information, deleting the original VM, and then re-creating it in a new availability set:
# Set variables
$resourceGroup = "myResourceGroup"
$vmName = "myVM"
$newAvailSetName = "myAvailabilitySet"
# Get the details of the VM to be moved to the availability set
$originalVM = Get-AzVM `
-ResourceGroupName $resourceGroup `
-Name $vmName
# Create a new availability set if it doesn't exist
$availSet = Get-AzAvailabilitySet `
-ResourceGroupName $resourceGroup `
-Name $newAvailSetName `
-ErrorAction Ignore
if (-Not $availSet) {
$availSet = New-AzAvailabilitySet `
-Location $originalVM.Location `
-Name $newAvailSetName `
-ResourceGroupName $resourceGroup `
-PlatformFaultDomainCount 2 `
-PlatformUpdateDomainCount 2 `
-Sku Aligned
}
# Remove the original VM
Remove-AzVM -ResourceGroupName $resourceGroup -Name $vmName
# Create the basic configuration for the replacement VM.
$newVM = New-AzVMConfig `
-VMName $originalVM.Name `
-VMSize $originalVM.HardwareProfile.VmSize `
-AvailabilitySetId $availSet.Id
# For a Linux VM, change the last parameter from -Windows to -Linux
Set-AzVMOSDisk `
-VM $newVM -CreateOption Attach `
-ManagedDiskId $originalVM.StorageProfile.OsDisk.ManagedDisk.Id `
-Name $originalVM.StorageProfile.OsDisk.Name `
-Windows
# Add data disks
foreach ($disk in $originalVM.StorageProfile.DataDisks) {
Add-AzVMDataDisk -VM $newVM `
-Name $disk.Name `
-ManagedDiskId $disk.ManagedDisk.Id `
-Caching $disk.Caching `
-Lun $disk.Lun `
-DiskSizeInGB $disk.DiskSizeGB `
-CreateOption Attach
}
# Add NICs and keep the same NICs as primary; keep the private IP too, if it exists
foreach ($nic in $originalVM.NetworkProfile.NetworkInterfaces) {
if ($nic.Primary -eq "True")
{
Add-AzVMNetworkInterface `
-VM $newVM `
-Id $nic.Id -Primary
}
else
{
Add-AzVMNetworkInterface `
-VM $newVM `
-Id $nic.Id
}
}
# Re-create the VM
New-AzVM `
-ResourceGroupName $resourceGroup `
-Location $originalVM.Location `
-VM $newVM `
-DisableBginfoExtension