Functions/Administration/Copy-CdsInstance.ps1

<#
    .SYNOPSIS
    Copy instance to another.
#>

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

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

        [Parameter(Mandatory=$true)]
        [ValidateSet("FullCopy", "MinimalCopy")]
        [String]
        $CopyType
    )
    begin {   
        $StopWatch = [System.Diagnostics.Stopwatch]::StartNew(); 
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters); 
        Assert-CdsAdminConnected;
    }    
    process {    
        $sourceInstance = Get-CdsInstance -Name $SourceInstanceDomainName;
        $targetInstance = Get-CdsInstance -Name $TargetInstanceDomainName;

        $copyToRequest = [pscustomobject]@{
                SourceEnvironmentId = $sourceInstance.Id
                TargetEnvironmentName = $targetInstance.DisplayName
                CopyType = $CopyType
        }

        $response = Copy-PowerAppEnvironment -EnvironmentName $targetInstance.Id -CopyToRequestDefinition $copyToRequest;
        if($response.Code)
        {
            throw "$($response.Error.code) : $($response.Error.message)";
        }
        $operationStatusUrl = $response.Headers["Operation-Location"];
        Watch-CdsOperation -OperationUrl $operationStatusUrl;
    }
    end {
        $StopWatch.Stop();
        Trace-CdsFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }    
}

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