Classes/TMASRReplication.ps1

using namespace Microsoft.Azure.Commands.RecoveryServices
using namespace Microsoft.Azure.Commands.Network.Models
using namespace Microsoft.Azure.Commands.RecoveryServices.SiteRecovery
using namespace Microsoft.Azure.Commands.ResourceManager.Cmdlets.SdkModels
using namespace Microsoft.Azure.Commands.Compute.Automation.Models

class TMASRReplication {
    [String]$SourceMachineName
    [String]$TargetMachineName
    [String]$TargetSubnet
    [String]$DiskType
    [String]$LogStorageAccountId
    [String]$Size
    [PSDiskEncryptionSet]$TargetDiskEncryptionSet
    [ARSVault]$TargetVault
    [AsrInMageAzureV2DiskInput[]]$Disks
    [PSResourceGroup]$TargetResourceGroup
    [PSVirtualNetwork]$TargetVirtualNetwork
    [ASRProtectionContainerMapping]$TargetContainerMap
    [ASRFabric]$Fabric
    [ASRProtectionContainer]$ProtectionContainer
    [ASRProtectableItem]$ProtectableVM
    [ASRJob]$ReplicationJob


    TMASRReplication() {}

    TMASRReplication([String]$SourceMachineName) {
        $this.SourceMachineName = $SourceMachineName
        $this.TargetMachineName = $SourceMachineName
    }

    [void] StartReplicationJob() {
        $ReplicationJobSplat = @{
            VMwareToAzure              = $true
            ProtectableItem            = $this.ProtectableVM
            Name                       = [Guid]::NewGuid().Guid
            ProtectionContainerMapping = $this.TargetContainerMap
            ProcessServer              = $this.Fabric.FabricSpecificDetails.ProcessServers[0]
            Account                    = $this.Fabric.FabricSpecificDetails.RunAsAccounts[0]
            RecoveryResourceGroupId    = $this.TargetResourceGroup.ResourceId
            RecoveryAzureNetworkId     = $this.TargetVirtualNetwork.Id
            RecoveryAzureSubnetName    = $this.TargetSubnet
            RecoveryVmName             = $this.TargetMachineName
            InMageAzureV2DiskInput     = $this.Disks
            Size                       = $this.Size
            # DiskType = $this.DiskType
            # -LogStorageAccountId $targetPostFailoverLogStorageAccount.Id `
        }
        $this.ReplicationJob = New-AzRecoveryServicesAsrReplicationProtectedItem @ReplicationJobSplat
    }

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

    [void] SetDiskMapping() {
        if ($this.ProtectableVM.Disks.Count -gt 1) {
            $i = 0
            ForEach ($Disk in $this.ProtectableVM.Disks) {

                if ($i -eq 0) {
                    $OsDiskSplat = @{
                        DiskID              = $this.ProtectableVM.OSDiskId
                        DiskType            = $this.DiskType
                        LogStorageAccountId = $this.LogStorageAccountId
                    }
                    if ($this.TargetDiskEncryptionSet) { $OsDiskSplat.DiskEncryptionSetID = $this.TargetDiskEncryptionSet.Id }
                    $OSDisk = New-AzRecoveryServicesAsrInMageAzureV2DiskInput @OsDiskSplat
                    $this.Disks += $OSDisk
                } else {
                    $DataDiskSplat = @{
                        DiskID              = $Disk.Id
                        DiskType            = $this.DiskType
                        LogStorageAccountId = $this.LogStorageAccountId
                    }
                    if ($this.TargetDiskEncryptionSet) { $DataDiskSplat.DiskEncryptionSetID = $this.TargetDiskEncryptionSet.Id }
                    $DataDisk = New-AzRecoveryServicesAsrInMageAzureV2DiskInput @DataDiskSplat
                    $this.Disks += $DataDisk
                }
                $i++
            }
        } else {
            # Create Disk Mapping for a single drive
            $DiskSplat = @{
                DiskID              = $this.ProtectableVM.OSDiskId
                DiskType            = $this.DiskType
                LogStorageAccountId = $this.LogStorageAccountId
            }
            if ($this.TargetDiskEncryptionSet) { $DiskSplat.DiskEncryptionSetID = $this.TargetDiskEncryptionSet.Id }
            $this.Disks = New-AzRecoveryServicesAsrInMageAzureV2DiskInput @DiskSplat
        }
    }

    [void] SetProtectionContainer() {
        Write-Host "Setting protection container reference for fabric server '$($this.Fabric.Name)-$($this.Fabric.FriendlyName)'"
        $this.ProtectionContainer = Get-AzRecoveryServicesAsrProtectionContainer -Fabric $this.Fabric
    }

    [void] SetProtectionContainer([ASRFabric]$Fabric) {
        Write-Host "Setting protection container reference for fabric server '$($Fabric.Name)-$($Fabric.FriendlyName)'"
        $this.ProtectionContainer = Get-AzRecoveryServicesAsrProtectionContainer -Fabric $Fabric
    }

    [void] SetProtectableItem() {
        $_protectableItem = [TMASRCommon]::GetProtectableItem($this.ProtectionContainer, $this.SourceMachineName)
        Write-Host "Setting protectable item reference '$($this.SourceMachineName)'"
        $this.ProtectableVM = $_protectableItem
    }


    # [PSObject] GetProtectedItemFromVault([String]$VaultName, [String]$SourceMachineName, [String]$SourceConfigurationServer) {
    # $vaultServer = $this.GetAndEnsureVaultContext($VaultName)
    # $FabricServer = $this.GetFabricServer($SourceConfigurationServer)
    # # $ProtectionContainer = $this.GetProtectionContainer($FabricServer)
    # $protectableVM = $this.GetProtectableItem($ProtectionContainer, $SourceMachineName)

    # Write-Host "ProtectableStatus: '$($protectableVM.ProtectionStatus)'"

    # if ($protectableVM.ReplicationProtectedItemId -ne $null) {
    # $ProtectedItem = $this.GetProtectedItem($ProtectionContainer, $SourceMachineName)

    # Write-Host "ProtectionState: '$($ProtectedItem.ProtectionState)'"
    # Write-Host "ProtectionDescription: '$($ProtectedItem.ProtectionStateDescription)'"

    # return $ProtectedItem
    # } else {
    # Write-Host "'$($SourceMachineName)' protectable item is not in a protected state ready for replication"

    # return $null
    # }
    # }
}