Learn What IT Pros Need to Know About Windows 11 - August 24th at 1 PM ET! Learn What IT Pros Need to Know About Windows 11 - August 24th at 1 PM ET!
Microsoft Azure

Restore Azure Virtual Machines to an Availability Set Using PowerShell

In this post, I am going to show you one way to restore an Azure virtual machine from Azure Backup. From there, we will join that machine to an existing availability set.



Sponsored Content

Read the Best Personal and Business Tech without Ads

Staying updated on what is happening in the technology sector is important to your career and your personal life but ads can make reading news, distracting. With Thurrott Premium, you can enjoy the best coverage in tech without the annoying ads.

The Problem

When you do a simple next > next > next restore of an Azure virtual machine from Azure Backup, the newly created machine does not join an availability set. That is a problem because you might have joined the original machine to an availability set with high higher levels of planned or unplanned outage tolerance. You would expect the restored machine to have the same levels of redundancy. However, it will not. There is not a “re-join the original availability set” option.

There are a few workarounds available to us now. In this post, I am going to focus on the original workaround. I will show you how to do a restore of the disks to a storage account only. There will not be a virtual machine created. Using those disks, we will have to create a new virtual machine in the availability set of choice.

This example is based on Azure Resource Manager or ARM.

Restore the Disks Using PowerShell

The first things you need to do with Azure PowerShell:

  1. Make sure you have the latest version of the PowerShell modules or you will get weird errors.
  2. Sign into Azure.
  3. Select the correct subscription.

In my example, I “accidentally” deleted a protected virtual machine called vm-petri-01. This was in a resource group called rg-petrisource and stored in a storage account called rgpetrisourcedisks369. I had previously configured backups to a recovery services vault called rsv-petri.

I will store a few variables in memory and they can easily be reused:

$VMResourceGroupName = "rg-petrisource"

$VMName = "vm-petri-01"

$StorageAccount = “rgpetrisourcedisks369”

You need to set a recovery services vault context. This is like marking a recovery services vault as the current working resource. I will store the vault in a variable called $Vault and then set the vault context.
$Vault = Get-AzureRmRecoveryServicesVault -Name “rsv-petri”

Set-AzureRmRecoveryServicesVaultContext -Vault $Vault

A virtual machine is backed up into a container. We will retrieve that container using the following line:
$NamedContainer = Get-AzureRmRecoveryServicesBackupContainer  -ContainerType AzureVM –Status Registered -FriendlyName $VMName

We will select the item backed up for that virtual machine from the container:
$BackupItem = Get-AzureRmRecoveryServicesBackupItem –Container $NamedContainer  –WorkloadType "AzureVM"

To do a restore, we must select a recovery point. The next line will retrieve all of the recovery points:
$RecoveryPoints = Get-AzureRmRecoveryServicesBackupRecoveryPoint -Item $BackupItem

The results of the above line are stored in an array. It is a variable with multiple selectable entries, one for each recovery point. I will display the available recovery points and I can pick one.


I am going to use the most recent recovery point, which is the first entry in the array (item 0). I can see more details about the recovery point by running:
$RecoveryPoints[0] | fl *

Now it is time to do the restore. I will save the results of the restore action to a variable and I can track the job’s status:
$RestoreJob = Restore-AzureRmRecoveryServicesBackupItem -RecoveryPoint $RecoveryPoints[0] -StorageAccountName $StorageAccount -StorageAccountResourceGroupName $ResourceGroupName

The job will take some time. You can track the job by querying the results of $RestoreJob.

Re-Create the Virtual Machine

The next step in this method is to create a new virtual machine using PowerShell. You could use JSON to create the new machine. There is a great script shared by Microsoft employee Rhoderick Milne. This will do the trick:

  1. Get the URI (the URL) of the restored OS disk from the storage account, which is easily found in the Azure Portal.
  2. Download the script.
  3. Modify the variables to account for things such as the desired machine name, disk URI, availability set name, and so on.
  4. Run the script.

When you browse your storage account, you will spot some very interesting BLOBs. Some JSON files are restored for you, which give you the ability to restore the virtual machine using a customizable JSON deployment!

A few minutes after running the script, you will have a new virtual machine with the restored operating system disk. If there were any data disks, you can attach the restored data disks to the virtual machine.

Related Topics:


Don't have a login but want to join the conversation? Sign up for a Petri Account

Comments (0)

Leave a Reply

Aidan Finn, Microsoft Most Valuable Professional (MVP), has been working in IT since 1996. He has worked as a consultant and administrator for the likes of Innofactor Norway, Amdahl DMR, Fujitsu, Barclays and Hypo Real Estate Bank International where he dealt with large and complex IT infrastructures and MicroWarehouse Ltd. where he worked with Microsoft partners in the small/medium business space.

Register for Advanced Microsoft 365 Day!

GET-IT: Advanced Microsoft 365 1-Day Virtual Conference - Live August 24th!

Join us on Tuesday, August 24th and hear from Microsoft MVPs and industry experts about how to take advantage of Microsoft 365 at a technical level and dive deep into the features and functionality that will make your environment more secure and compliant.


Sponsored By