In Azure ,can we add the existing VM into Availability Set or Availability Zone

+1 vote
Dec 17, 2019 in Azure by DHISHYANTH
• 130 points
508 views

1 answer to this question.

0 votes

Hi @Dhishyanth.

You can delete the vm and recreate in a different availability set. You can use the following powershell code:

# 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 new availability set if it does not 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
    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 NIC(s) and keep the same NIC as primary
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 
                }
  }

 #Recreate the VM
    New-AzVM `
   -ResourceGroupName $resourceGroup `
   -Location $originalVM.Location `
  -VM $newVM `
   -DisableBginfoExtension
answered Dec 18, 2019 by Kalgi
• 51,850 points

Related Questions In Azure

0 votes
1 answer

How to add Azure Virtual machine to an existing availability set?

You cannot associate an Availability Set to an existing ...READ MORE

answered Aug 9, 2018 in Azure by null_void
• 3,220 points
3,039 views
0 votes
2 answers

How can I view the deployed files in Azure?

In Visual Studio, in the window "Server ...READ MORE

answered Aug 21, 2018 in Azure by Priyaj
• 57,530 points
826 views
+1 vote
10 answers

How can the NuGet packages be used in Azure Functions?

Yes! Although the Azure Functions portal does ...READ MORE

answered Jun 8, 2018 in Azure by club_seesharp
• 3,450 points
8,015 views
0 votes
1 answer

How can we rewrite an URL in Azure Webapp?

You need to create a web.config file ...READ MORE

answered Jun 14, 2018 in Azure by null_void
• 3,220 points
2,793 views
0 votes
1 answer

Can we have more than one Public IP in Azure?

It appears you can now have multiple ...READ MORE

answered Aug 22, 2018 in Azure by null_void
• 3,220 points
57 views
0 votes
2 answers

Can I Login without prompt in Azure?

You can use -Credential parameter, and DPAPI to login. First, ...READ MORE

answered Aug 17, 2018 in Azure by Priyaj
• 57,530 points
2,598 views
0 votes
2 answers

How can I download a .vhd image to my local machine from azure and upload the same to a different azure account?

From the Windows Azure Portal you can ...READ MORE

answered Aug 20, 2018 in Azure by Priyaj
• 57,530 points
6,379 views
0 votes
1 answer

How can I remove/hide/disable excessive HTTP response headers in Azure/IIS7 without having to use UrlScan?

MSDN published an article on how to ...READ MORE

answered May 21, 2018 in Azure by club_seesharp
• 3,450 points
1,227 views