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
1,277 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
• 52,310 points

Related Questions In Azure

0 votes
1 answer

Where i can see the monitoring data in Azure? Can we get that data locally in SQL or NoSQL?

You can check the logs for the ...READ MORE

answered Jun 30, 2020 in Azure by Shivangi Namgyal
118 views
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
5,290 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
• 58,120 points
2,247 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
11,469 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
5,286 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
133 views
0 votes
1 answer
0 votes
1 answer
0 votes
0 answers

Is there any way for accessing the pipeline in Azure VM

I want to know like I have ...READ MORE

Sep 21, 2020 in Azure by nidhi
• 120 points
82 views
0 votes
1 answer

How to set the subcription in the Azure account?

Hi@akhtar, You can set your required subscription to ...READ MORE

answered Nov 10, 2020 in Azure by MD
• 95,180 points
42 views