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 2

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 3

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 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 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

2 thoughts on “How To Find The Recovery Services Vault For an Azure VM With PowerShell”

Leave a comment...

  1. how to get backup policy name through azure powershell

  2. 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!

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

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

Subscribe to Stay in Touch

Never miss out on your favorite posts and our latest announcements!

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!