Functions/Instances/Copy-CdsInstances.ps1

<#
    .SYNOPSIS
    Copy instance to another.
#>

function Copy-CdsInstances {
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory, ValueFromPipeline)]
        [String]
        $SourceInstanceName,

        [Parameter(Mandatory=$true)]
        [String]
        $TargetInstanceName,

        [Parameter(Mandatory=$false)]
        [DateTime]
        $RestoreTimeUtc
    )
    begin {   
        $StopWatch = [System.Diagnostics.Stopwatch]::StartNew(); 
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters); 
        Assert-CdsIsAdmin;
    }    
    process {

        if(-not $RestoreTimeUtc)
        {
            $RestoreTimeUtc = Get-Date;
        }

        $sourceInstance = Get-CdsInstance -Name $SourceInstanceName;
        $targetInstance = Get-CdsInstance -Name $TargetInstanceName;

        $credentials = $Global:CdsContext.Credentials;
        $apiUrl = $Global:CdsContext.AdminApiUrl;
        $operation = Restore-CrmInstance -ApiUrl $apiUrl -Credential $credentials -SourceInstanceId $sourceInstance.Id -TargetInstanceId $targetInstance.Id -RestoreTimeUtc $RestoreTimeUtc;
        $operation.OperationId;
    }
    end {
        $StopWatch.Stop();
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }    
}

Export-ModuleMember -Function Copy-CdsInstances -Alias *;