src/Administration/Restore-XrmInstance.ps1

<#
    .SYNOPSIS
    Restore instance at given time.

    .DESCRIPTION
    Restore a backup of given instance to itself or another instance.
    Give either BackupLabel (a labelled backup, see Get-XrmBackup) or RestoreTimeUtc (a point in time); without them, use Copy-XrmInstance.

    .PARAMETER SourceInstanceDomainName
    Instance domain name (myinstance => myinstance.crm.dynamics1.com) that you want to restore

    .PARAMETER TargetInstanceDomainName
    Instance domain name (myinstance => myinstance.crm.dynamics1.com) where you want to restore. (Default: the source instance)

    .PARAMETER TargetInstanceNewDisplayName
    Display name given to the target instance after the restore. (Default: the current display name of the target instance)

    .PARAMETER TargetInstanceSecurityGroupId
    AAD Security Group ID to define on target instance to restrict users access

    .PARAMETER RestoreTimeUtc
    Date time in UTC of restore point

    .PARAMETER BackupLabel
    Name of the backup

    .OUTPUTS
    System.Void. The progress of the restore is written to the host and to the log until the operation ends (see Watch-XrmOperation).

    .EXAMPLE
    Connect-XrmAdmin -TenantId $tenantId -ApplicationId $applicationId -ClientSecret $clientSecret;
    Restore-XrmInstance -SourceInstanceDomainName "contoso-prod" -TargetInstanceDomainName "contoso-uat" -BackupLabel "Before release 2.4";

    .EXAMPLE
    Restore-XrmInstance -SourceInstanceDomainName "contoso-prod" -TargetInstanceDomainName "contoso-uat" -RestoreTimeUtc ([DateTime]::UtcNow.AddHours(-2)) -TargetInstanceNewDisplayName "Contoso UAT (restored)";

    .LINK
    https://github.com/AymericM78/PowerDataOps/blob/main/documentation/commands/Restore-XrmInstance.md
#>

function Restore-XrmInstance {
    [CmdletBinding(SupportsShouldProcess, ConfirmImpact = "Medium")]
    [OutputType([System.Void])]
    param
    (
        [Parameter(Mandatory, ValueFromPipeline)]
        [String]
        $SourceInstanceDomainName,

        [Parameter(Mandatory = $false)]
        [ValidateNotNullOrEmpty()]
        [String]
        $TargetInstanceDomainName,

        [Parameter(Mandatory = $false)]
        [ValidateNotNullOrEmpty()]
        [String]
        $TargetInstanceNewDisplayName,

        [Parameter(Mandatory = $false)]
        [ValidateNotNullOrEmpty()]
        [Guid]
        $TargetInstanceSecurityGroupId,

        [Parameter(Mandatory = $false)]
        [ValidateNotNullOrEmpty()]
        [DateTime]
        $RestoreTimeUtc,

        [Parameter(Mandatory = $false)]
        [ValidateNotNullOrEmpty()]
        [string]
        $BackupLabel
    )
    begin {   
        $StopWatch = [System.Diagnostics.Stopwatch]::StartNew(); 
        Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Start -Parameters ($MyInvocation.MyCommand.Parameters); 
        Assert-XrmAdminConnected;
    }    
    process {

        $restoreTimeUtcValue = "";
        if ($PSBoundParameters.BackupLabel) {
            $backup = Get-XrmBackup -InstanceDomainName $SourceInstanceDomainName -BackupLabel $BackupLabel;
            if (-not $backup) {
                throw "No backup found for label '$BackupLabel'";
            }
            $restoreTimeUtcValue = $backup.backupPointDateTime;
        }
        else {
            if (-not $PSBoundParameters.RestoreTimeUtc) {
                throw "Must use Copy-XrmInstance instead of Restore-XrmInstance!";
            }
            $restoreTimeUtcValue = $RestoreTimeUtc.ToString("yyyy-MM-dd HH:mm:ss");
        }

        $sourceInstance = Get-XrmInstance -Name $SourceInstanceDomainName;
        $restoreRequest = [pscustomobject]@{
            SourceEnvironmentId  = $sourceInstance.Id;
            RestorePointDateTime = $restoreTimeUtcValue
        }

        if ($PSBoundParameters.TargetInstanceDomainName) {
            $targetInstance = Get-XrmInstance -Name $TargetInstanceDomainName;
        }
        else {
            $targetInstance = $sourceInstance;
        }

        if ($PSBoundParameters.TargetInstanceNewDisplayName) {
            $restoreRequest | Add-Member -MemberType NoteProperty -Name "TargetEnvironmentName" -Value $TargetInstanceNewDisplayName;
        }
        else {
            $restoreRequest | Add-Member -MemberType NoteProperty -Name "TargetEnvironmentName" -Value $targetInstance.DisplayName;
        }

        if ($PSBoundParameters.TargetInstanceSecurityGroupId) {
            $restoreRequest | Add-Member -MemberType NoteProperty -Name "TargetSecurityGroupId" -Value $TargetInstanceSecurityGroupId;
        }

        if (-not $PSCmdlet.ShouldProcess($targetInstance.Id, "Overwrite environment with a backup")) { return; }
        $response = Restore-PowerAppEnvironment -EnvironmentName $targetInstance.Id -RestoreToRequestDefinition $restoreRequest -WaitUntilFinished $false;
        if ($response.Code) {
            throw "$($response.Error.code) : $($response.Error.message)";
        }
        $operationStatusUrl = $response.Headers["Operation-Location"];
        Watch-XrmOperation -OperationUrl $operationStatusUrl;
        
    }
    end {
        $StopWatch.Stop();
        Trace-XrmFunction -Name $MyInvocation.MyCommand.Name -Stage Stop -StopWatch $StopWatch;
    }    
}

Export-ModuleMember -Function Restore-XrmInstance -Alias *;