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 is 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 enabling it!" 
        } 
    }
}

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

Jump into the 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.

How to Find The Recovery Services Vault for an Azure VM with PowerShell 1

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

How to Find The Recovery Services Vault for an Azure VM with PowerShell 2

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 its 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 searching 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 helping 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-

Related Posts

Previous

How to Copy Files From One Azure Storage Account to Another

Disaster Recovery and Replication for Hyper-V VMs Using Altaro VM Backup

Next

4 thoughts on “How to Find The Recovery Services Vault for an Azure VM with PowerShell”

Leave a comment...

  1. Hello Rudraraju, here is how to get the Azure Backup Policy name with PowerShell:
    $vault = Get-AzRecoveryServicesVault -ResourceGroupName "Resource Group Name" -Name "Recovery Services vault Name" Set-AzRecoveryServicesVaultContext -Vault $vault (Get-AzRecoveryServicesBackupProtectionPolicy).Name
    Hope this helps!

  2. Hello Pankaj, thanks for the comment!
    Unfortunately, it is not possible today to get the VM backup size with Azure PowerShell. However, you can get the backup size for each backup job by using the Get-AzRecoveryServicesBackupJobDetail cmdlet.
    Could you please share the Azure CLI command that shows the VM Backup Size?
    Thanks!

Let me know what you think, or ask a question...

The content of this website is copyrighted from being plagiarized!

You can copy from the 'Code Blocks' in 'Black' by selecting the Code.

Please send your feedback to the author using this form for any 'Code' you like.

Thank you for visiting!