Classes/TMASRCommon.ps1

using namespace Microsoft.Azure.Commands.RecoveryServices
using namespace Microsoft.Azure.Commands.RecoveryServices.SiteRecovery

class TMASRCommon {

    static [ARSVault] GetAndEnsureVaultContext() {
        Write-Host "Ensuring services vault context"
        $TargetVault = Get-AzRecoveryServicesVault
        if (($null -eq $TargetVault) -or ($TargetVault.Count -gt 1)) {
            Write-Error "Unable to find vault without a provided name"
        }
        [void](Set-AzRecoveryServicesAsrVaultContext -Vault $TargetVault)
        return $TargetVault
    }

    static [ARSVault] GetAndEnsureVaultContext([String]$VaultName) {
        Write-Host "Ensuring services vault context '$($VaultName)'"
        $TargetVault = Get-AzRecoveryServicesVault -Name $VaultName
        if ($null -eq $TargetVault) {
            Write-Error "Unable to find vault with name '$($VaultName)'"
        }
        [void](Set-AzRecoveryServicesAsrVaultContext -Vault $TargetVault)
        return $TargetVault
    }

    static [ASRFabric] GetFabric() {
        Write-Host "Getting fabric server"
        $Fabric = Get-AzRecoveryServicesAsrFabric | Where-Object { $_.FabricType -ne 'HyperVSite' }
        if ((-not $Fabric) -or ($Fabric.Count -gt 1)) {
            Write-Error "Could not determine fabric without a provided configuration server name"
        }
        return $Fabric
    }

    static [ASRFabric] GetFabric([String]$SourceConfigurationServer) {
        Write-Host "Getting fabric for configuration server '$($SourceConfigurationServer)'"
        $_Fabric = Get-AzRecoveryServicesAsrFabric -FriendlyName $SourceConfigurationServer
        if ((-not $_Fabric) -or ($_Fabric.Count -gt 1)) {
            Write-Error "Could not determine fabric with the provided configuration server name '$($SourceConfigurationServer)'"
        }
        return $_Fabric
    }

    static [ASRProtectionContainer] GetProtectionContainer([ASRFabric]$Fabric) {
        Write-Host "Getting protection container reference for fabric server '$($Fabric.Name)-$($Fabric.FriendlyName)'"
        $_protectionContainer = Get-AzRecoveryServicesAsrProtectionContainer -Fabric $Fabric
        return $_protectionContainer
    }

    static [ASRReplicationProtectedItem] GetProtectedItem([ASRProtectionContainer]$ProtectionContainer, [String]$SourceMachineName) {
        Write-Host "Getting protected item reference '$($SourceMachineName)'"
        $ProtectedItemSplat = @{
            ProtectionContainer = $ProtectionContainer
            FriendlyName        = $SourceMachineName
        }
        $ProtectedItem = Get-AzRecoveryServicesAsrReplicationProtectedItem @ProtectedItemSplat
        return $ProtectedItem
    }


    static [ASRProtectableItem] GetProtectableItem([ASRProtectionContainer]$ProtectionContainer, [String]$SourceMachineName) {
        Write-Host "Getting protectable item reference '$($SourceMachineName)'"
        $ProtectableItemSplat = @{

        }
        $_protectableVM = Get-AzRecoveryServicesAsrProtectableItem -ProtectionContainer $ProtectionContainer -FriendlyName $SourceMachineName
        return $_protectableVM
    }

    static [ASRProtectionContainerMapping] GetProtectionContainerMapping($ProtectionContainer) {
        $ContainerMap = Get-AzRecoveryServicesAsrProtectionContainerMapping -ProtectionContainer $ProtectionContainer
        if ($null -eq $ContainerMap) {
            throw "Container map was not found"
        }

        return $ContainerMap
    }

    static [ASRProtectionContainerMapping] GetProtectionContainerMapping($ProtectionContainer, $ReplicationPolicy) {
        $ContainerMap = Get-AzRecoveryServicesAsrProtectionContainerMapping -ProtectionContainer $ProtectionContainer |
            Where-Object { $_.PolicyFriendlyName -eq $ReplicationPolicy }
        if ($null -eq $ContainerMap) {
            throw "Container map '$($ReplicationPolicy)' was not found"
        }
        return $ContainerMap
    }
}