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

What is “Inside Microsoft Teams”?

“Inside Microsoft Teams” is a webcast series, now in Season 4 for IT pros hosted by Microsoft Product Manager, Stephen Rose. Stephen & his guests comprised of customers, partners, and real-world experts share best practices of planning, deploying, adopting, managing, and securing Teams. You can watch any episode at your convenience, find resources, blogs, reviews of accessories certified for Teams, bonus clips, and information regarding upcoming live broadcasts on our website.

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.
External Sharing and Guest User Access in Microsoft 365 and Teams

This eBook will dive into policy considerations you need to make when creating and managing guest user access to your Teams network, as well as the different layers of guest access and the common challenges that accompany a more complicated Microsoft 365 infrastructure.

You will learn:

  • Who should be allowed to be invited as a guest?
  • What type of guests should be able to access files in SharePoint and OneDrive?
  • How should guests be offboarded?
  • How should you determine who has access to sensitive information in your environment?

Sponsored by:

Office 365 Coexistence for Mergers & Acquisitions: Don’t Panic! Make it SimpleLive Webinar on Tuesday, November 16, 2021 @ 1 pm ET

In this session, Microsoft MVPs Steve Goodman and Mike Weaver, and tenant migration expert Rich Dean, will cover the four most common steps toward Office 365 coexistence and explain the simplest route to project success.

  • Directory Sync/GAL Sync – How to prepare for access and awareness
  • Calendar Sharing – How to retrieve a user’s shared calendar, or a room’s free time
  • Email Routing – How to guarantee email is routed to the active mailbox before and after migration
  • Domain Sharing – How to accommodate both original and new SMTP domains at every stage

Aimed at IT Admins, Infrastructure Engineers and Project Managers, this session outlines both technical and project management considerations – giving you a great head start when faced with a tenant migration.the different layers of guest access and the common challenges that accompany a more complicated Microsoft 365 infrastructure.

Sponsored by: