Can the ASR Vault Id, Fabric and Container name from an Azure VM be gotten programmatically?

Viewed 644

I'm trying to disable the Azure Site Recovery Protection from Azure VMs through Azure Powershell (Az Module). The idea is to get the VM using the name and Resource Group:

Get-AzVM -Name $name -ResourceGroupName $rgName

And then disable the protection using any linking data that connects the VM to the ASR Protected Item.

As per my understanding, this can be done using the following cmdlet:

Remove-AzRecoveryServicesAsrReplicationProtectedItem

This cmdlet requires a Replication Protected Item Object:

Get-AzRecoveryServicesAsrReplicationProtectedItem

Which requires a Protection Container, which in turn requires a Fabric, which, lastly, requires the Vault.

Of course, I can get these parameters and objects if I know them beforehand. But that would defeat the purpose of creating a script that resolves them programmatically by using the Azure Virtual Machine name and RG.

I can't find any way of linking or getting this information from the Azure VM object...

I had to resolve a somewhat similar situation with Backups, and I achieved it using:

Get-AzRecoveryServicesBackupStatus -Name $vmname -ResourceGroupName $rg -Type "AzureVM"

$container = (Get-AzRecoveryServicesBackupContainer -ContainerType "AzureVM" -Status "Registered" -VaultId $VaultID | where-object {$_.FriendlyName -eq $vmname -and $_.ResourceGroupName -eq $rg})

But I can't find any cmdlet similar to that one which would take the VM Object or at least the VM name and RG name as parameters...

  1. Can this be done?
  2. How does the Portal links the VM and the ASR Item when you select Disaster Recovery from the VM Dashboard?
1 Answers

Sadly there's no direct approach for this. I've reported this in Azure-Powershell Github.

In the meantime, the solution I found is:

  1. Loop through every single Recovery Services Vault
  2. Then through every Fabric
  3. Then through every Container
  4. Then search for the ProtectedItem.FriendlyName that matches your VM name

This is terrible, performance-wise, but it seems like the only current way of solving it I could find.

foreach ($Vault in $Vaults) 
    {
        Set-AzRecoveryServicesAsrVaultContext -Vault $Vault -InformationAction SilentlyContinue
        $Fabrics = Get-AzRecoveryServicesAsrFabric
            foreach ($Fabric in $Fabrics){
                $drContainers = Get-AzRecoveryServicesAsrProtectionContainer -Fabric $Fabric
                    foreach ($drContainer in $drContainers){
                        $protectedItem = Get-AzRecoveryServicesAsrReplicationProtectedItem -ProtectionContainer $drContainer -FriendlyName $vm.Name -ErrorAction SilentlyContinue
                        if ($null -ne $protectedItem){
                            return $protectedItem
                        }

                    }
            }
    }
Related