ASR - How do you get a list of all the VMs in a Recovery Plan with powershell ?

srv-asr 20 Reputation points
2024-02-23T16:25:35.9066667+00:00

I can get a recovery plan with powershell :

$RecoveryPlan = Get-AzRecoveryServicesAsrRecoveryPlan | where {$_.name -eq $rpName}

-- But how do I get a list of all the VMs in this plan with powershell? thanks

Azure Site Recovery
Azure Site Recovery
An Azure native disaster recovery service. Previously known as Microsoft Azure Hyper-V Recovery Manager.
672 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,274 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Marcin Policht 16,965 Reputation points MVP
    2024-02-23T17:36:20.8533333+00:00

    Try the following:

    # Get the Recovery Plan
    $recoveryPlan = Get-AzRecoveryServicesAsrRecoveryPlan -ResourceGroupName $resourceGroupName -VaultName $vaultName -Name $rpName
    
    # Loop through each replication protection container in the Recovery Plan
    foreach ($containerId in $recoveryPlan.ReplicationProtectionContainers) {
        # Get the protected items (VMs) in each replication protection container
        $protectedItems = Get-AzRecoveryServicesAsrReplicationProtectedItem -ResourceGroupName $resourceGroupName -VaultName $vaultName -ProtectionContainerId $containerId.Id
    
        # Display information about each protected item
        foreach ($item in $protectedItems) {
            Write-Host "VM Name: $($item.FriendlyName)"
            Write-Host "VM ID: $($item.Id)"
            Write-Host "-------------------------"
        }
    }
    
    

    hth Marcin


  2. srv-asr 20 Reputation points
    2024-02-23T19:36:15.2333333+00:00

    I found an article here, https://00shep.blogspot.com/2019/07/azure-script-audit-recovery-plans-for.html and borrowed some of the code.

    `$RecoveryPlan = Get-AzRecoveryServicesAsrRecoveryPlan | where {$_.name -eq $rpName

    #get all replicated items $ReplicationProtectedItem = Get-AzRecoveryServicesAsrReplicationProtectedItem -ProtectionContainer $PrimaryProtContainer #Array to house the protected items it plan $ObjectArray = New-Object System.Collections.Generic.List[System.Object] foreach($group in $plandetails.Groups) { foreach ($groupprotecteditem in $group.ReplicationProtectedItems) { $VMmatch = Get-AzRecoveryServicesAsrProtectableItem -ProtectionContainer $asrcontainer | where { $.ProtectionStatus -eq 'Protected' -and $.ReplicationProtectedItemId -eq $groupprotecteditem.id} | Select -expand FriendlyName
    $tempArray = New-Object System.Object $tempArray | Add-Member -MemberType NoteProperty -Name "VMName" -Value $VMmatch $tempArray | Add-Member -MemberType NoteProperty -Name "RecoveryPlan" -Value $recoveryplan $tempArray | Add-Member -MemberType NoteProperty -Name "Group" -Value $group.name $tempArray | Add-Member -MemberType NoteProperty -Name "ProtectedItem" -Value $groupprotecteditem $tempArray | Add-Member -MemberType NoteProperty -Name "ID" -Value $groupprotecteditem.id $ObjectArray.add($tempArray) } }

    #$ObjectArray gives IDs - get the IDs and compare to Names in $replicateditems. $planVMs= @() foreach ($obObject in $ObjectArray.id) { $row = "" | select "VMNAME","ID" $obid = ($obObject -split "/")[14] $vm = ($ReplicationProtectedItem | where {$_.name -eq $obid}).FriendlyName $row."VMNAME" = $vm $row."ID" = $obid $planVMs += $row } and the result of $planVMs is $planVMs VMNAME ID


    ASRTEST01 myvc01_503d77c6-c646-5faa-c4da-92f2cd3d2731 ASRTEST02 myvc01_503d9413-2e92-5ea6-3032-616bacd8f8ae`

    0 comments No comments