How To Find The Recovery Services Vault For an Azure VM With PowerShell

3 min read

Introduction

Azure Backup is the Azure-based service you can use to back up (or protect) and restore your data in the Microsoft cloud. Azure Backup replaces your existing on-premises or off-site backup solution with a cloud-based solution that is reliable, scalable, secure, and cost-competitive.

You come across the following scenario:

You have a large number of Azure VMs and more than 50 Recovery Services Vault that are protecting those VMs, you need a quick way to find the Recovery Services Vault the VM is a member of. This would help if you want to change the backup policy for a given VM as an example.

Find The Recovery Services Vault

In this quick article, I will show you how to quickly find the Recovery Services Vault the VM is a member of with PowerShell, so you can take further actions if needed.

Here is the PowerShell script using the latest Az Module commands that will do the job for you. The script will go through all your Azure subscriptions to locate the VM and then quickly find the Recovery Services Vault that is a member of.

<#
.Synopsis
A script used to find the Recovery Services Vault of an Azure VM

.DESCRIPTION
A script used to find the Recovery Services Vault of an Azure VM,
The output will show the name of the Vault and tell you if the VM is protected or not.

.Notes
Created   : 2019-08-06
Version   : 1.0
Author    : Charbel Nemnom
Twitter   : @CharbelNemnom
Blog      : https://charbelnemnom.com
Disclaimer: This script is provided "AS IS" with no warranties.
#>

Do {
    $VMName = Read-Host "Enter VM Name"
} while (!$VMName)

$azSubs = Get-AzSubscription | Where-Object { $_.Name -notlike "*Azure Active Directory*" }

$VM = 0
foreach ( $azSub in $azSubs ) {
    Set-AzContext -Subscription $azSub | Out-Null
    $VMInfo = Get-AzVM -Name $VmName
    if ($VMInfo) {
        $VM++
        $status = Get-AzRecoveryServicesBackupStatus -Name $VMname -ResourceGroupName $VMInfo.ResourceGroupName -Type "AzureVM"
        If ($status.VaultId) {
            $rsv = $status.VaultId.Split('/')[-1]    
            Write-Output "The VM < $VMName > is member of RSV < $rsv >"    
        }
        If ($status.BackedUp -eq $false) {
            Write-Output "The VM < $VMName > is not protected with Azure Backup. You should consider enable it!" 
        } 
    }
}

If ($VM -eq 0) {
    Write-Warning "The Azure VM < $VMName > cannot be found. Please check your virtual machine name!"    
}

Jump into Azure Cloud Shell session (https://shell.azure.com) and run the script above.

Here is the output showing the VM is a member of a particular Recovery Services Vault, you can build upon this and change the backup policy if you want.

Here is another example showing the VM is not protected, you can build upon this and enable Azure Backup.

Last but definitely not least, if the VM name is not correct, you also get a message.

That’s it there you have it!

Summary

In this article, I showed you how to find the Recovery Services Vault the VM is a member of with PowerShell, so you can quickly identify it and update it’s backup configuration policy if needed. If the VM is not protected, you can. This script is optimized to give you the backup details for the VM instead of looping through all Recovery Services Vault and search for the VM.

Please note that you can also use the REST API of Azure Backup or Azure CLI to accomplish the same thing. Personally, I prefer to use Azure PowerShell.

This is version 1.0, do you want additional features? Please feel free to leave a comment below.

Do you want to explore the Azure Backup service in a deeper way, diving into the finer details of how things work, and to help people understand where it differs from what we traditionally used to do in the backup world? I highly recommend checking Azure Backup Deep Dive – Free Whitepaper.

__
Thank you for reading my blog.

If you have any questions or feedback, please leave a comment.

-Charbel Nemnom-

Advertisements
About Charbel Nemnom 475 Articles
Charbel Nemnom is a Cloud Architect and Microsoft Most Valuable Professional (MVP), totally fan of the latest's IT platform solutions, accomplished hands-on technical professional with over 17 years of broad IT Infrastructure experience serving on and guiding technical teams to optimize performance of mission-critical enterprise systems. Excellent communicator adept at identifying business needs and bridging the gap between functional groups and technology to foster targeted and innovative IT project development. Well respected by peers through demonstrating passion for technology and performance improvement. Extensive practical knowledge of complex systems builds, network design and virtualization.

Be the first to comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.