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

+1 vote
Dec 18, 2019 in Azure by DHISHYANTH
• 130 points
2,548 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


Hope this helps!!

If you need to know more about Azure, enroll with Microsoft Azure training course today.

Thank you!!

answered Dec 18, 2019 by Kalgi
• 52,350 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
728 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 10, 2018 in Azure by null_void
• 3,220 points
7,880 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,020 points
4,940 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
16,902 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
8,710 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 23, 2018 in Azure by null_void
• 3,220 points
758 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 22, 2020 in Azure by nidhi
• 120 points
559 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,460 points
620 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP