Get the list of all VM's with disaster recovery activation status in Azure

Aivaras 21 Reputation points
2022-07-18T10:47:40.87+00:00

Hello,

Could you please help us to get solution in exporting a list of all VM's with Disaster Recovery status?

At the moment there is no resource graph query available to find DR activation status. So we have to check it manually on each VM.

Thank you!

Azure Site Recovery
Azure Site Recovery
An Azure native disaster recovery service. Previously known as Microsoft Azure Hyper-V Recovery Manager.
712 questions
{count} votes

Accepted answer
  1. Andreas Baumgarten 110.2K Reputation points MVP
    2022-07-19T07:27:43.507+00:00

    Hi @Aivaras ,

    maybe this helps to get started: https://charbelnemnom.com/how-to-find-the-recovery-services-vault-for-an-azure-vm-with-powershell/

    Instead of querying one VM you can loop through your subscriptions and VMs with using the foreach cmdlet.

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Steffan Røstvig 31 Reputation points
    2023-05-26T11:33:19.4533333+00:00

    Here is what I did. I had to do multiple loops to get all the info 😅

    Script supports multiple subscriptions and recovery service vaults. I also query for a TAG on the VM. Output to a CSV file.

    # Connect to Azure
    Connect-AzAccount
    
    # Get all Azure subscriptions
    $subscriptions = Get-AzSubscription
    
    # Create an array to store the results
    $results = @()
    
    # Loop through each subscription
    foreach ($subscription in $subscriptions) {
        
        # Select the current subscription
        Set-AzContext -SubscriptionId $subscription.Id
    
    # Get all vaults
    $vaults = Get-AzRecoveryServicesVault
    
    # Loop through each vault
    foreach ($vault in $vaults) {
        
        # Select the current vault
        Set-AzRecoveryServicesAsrVaultContext -Vault $vault
    
    # Get all fabrics
    $fabrics = Get-AzRecoveryServicesAsrFabric
    
    # Loop through each fabric
    foreach ($fabric in $fabrics) {
        
        # Get the asr container
        $container = Get-AzRecoveryServicesAsrProtectionContainer -Fabric $fabric
        
        # Get protected Items
        $items= Get-AzRecoveryServicesAsrReplicationProtectedItem -ProtectionContainer $container
    
    # Loop through each item
    foreach ($item in $items) {
    
            # Check if replication is enabled for the virtual machine
            if ($item -ne $null) {
            
            # Get VM object to be able to populate with VM parameters
            $vm = get-azvm | Where-Object { $_.Name -match $item.FriendlyName}
            
                $result = [PSCustomObject]@{
                    "Subscription" = $subscription.Name
                    "RecoveryVault" = $vault.Name
                    "Fabric" = $fabric.Name
                    "VirtualMachineName" = $item.FriendlyName
                    "AppName" = $vm.Tags.AppName
                    "ActiveLocation" = $item.ActiveLocation
                    "ProtectionState" = $item.ProtectionState
                    "ReplicationHealth" = $item.ReplicationHealth
    
                } 
                $results += $result
                } #end if
    
    } #end VM loop
    
    } #end fabric loop
    
    } #end vault loop
    
    } #end sub loop
    
    # Output the results to a CSV file
    $results | Export-Csv -Path "VirtualMachineReplicationStatus.csv" -NoTypeInformation
    
    2 people found this answer helpful.

  2. Jackson Martins 10,556 Reputation points MVP
    2022-07-18T12:14:09.55+00:00

    Hi @Aivaras
    You can use Azure Powershell (CLI) on the azure portal, the sintax is just like:

    Get-AzRecoveryServicesAsrEvent -Name "VmMonitoringEvent;9091897569816476200_84576304-bafc-4714-8ba6-197a5d09d84f"

    reference: https://zcusa.951200.xyz/en-us/powershell/module/az.recoveryservices/get-azrecoveryservicesasrevent?view=azps-8.1.0#code-try-5

    If you prefer here display all the cmdlets

    reference: https://zcusa.951200.xyz/en-us/powershell/module/az.recoveryservices/?view=azps-8.1.0

    Get in touch if you need more help with this issue.

    --please don't forget to "Accept the answer" if the reply is helpful--

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.