DifferentialMigration/vSphere/vSphereDifferentialMigration.psm1

using module '../../Common/Result'
Import-Module -Name @(Join-Path $PSScriptRoot .. | Join-Path -ChildPath DifferentialMigration)
function Start-RMInteractiveVSphereVMBasedDifferentialMigration {
    param()

    $UserInput = @{}
    $OrganizationId = Get-Variable -Name "RMContext-CurrentProjectId" -ValueOnly
    $Migration, $Source = Read-RMFullMigrationId -VMBasedSourceRequired $true
    $UserInput.Add("Migration", $Migration)
    $UserInput.Add("Source", $Source)
    [RMMigrationReturn] $RMMigrationReturn = [RMMigrationReturn]::new()
    if ((Test-RMSourceHasRunningMigration -OrganizationId $OrganizationId -SourceId $Source.id)) {
        $UserMessage = "Currently there is a migration running for the source of the given migration ID, this differential migration might fail."
        Write-Warning $UserMessage
        $RMMigrationReturn.AddRMWarning([RMWarning]::new($UserMessage))
    }

    $ScheduledAt = Read-RMMigrationSchedule -IsDifferentialMigration $true
    $TransferMode = "run-once"
    if ("Continuous" -ieq $ScheduledAt) {
        $TransferMode = "continuous"
        $ScheduledAt = ""
    } else {
        $UpgradeTools = Read-RMBoolean -UserMessage "Do you want to upgrade the VM tools on the target VM" -DefaultValue $false
        $ShutdownSource = Read-RMBoolean -UserMessage "Shutdown source after data is fully migrated" -DefaultValue $false
        $ShutdownTarget = Read-RMBoolean -UserMessage "Shutdown target after data is fully migrated" -DefaultValue $false
        $UserInput.Add("UpgradeTools", $UpgradeTools)
        $UserInput.Add("ShutdownSource", $ShutdownSource)
        $UserInput.Add("ShutdownTarget", $ShutdownTarget)
    }
    $UserInput.Add("ScheduledAt", $ScheduledAt)
    $UserInput.Add("TransferMode", $TransferMode)

    $FinalizeMigration = Read-RMBoolean -UserMessage "Remove snapshot(s) from the Target VM in preparation for a cutover" -DefaultValue $false
    $UserInput.Add("FinalizeMigration", $FinalizeMigration)

    $ReadValue = Read-RMPair -UserMessage "Enter migration instructions in the format 'key=value' and separated by commas" `
        -Separator "=" -DefaultValue "None"
    $MigrationInstructions = Get-RMStringAsHashtable -InputString $ReadValue
    $UserInput.Add("MigrationInstruction", $MigrationInstructions)

    $IgnoreValidationErrors = Read-RMBoolean -UserMessage "Ignore validation errors" -DefaultValue "false"
    $UserInput.Add("IgnoreValidationErrors", $IgnoreValidationErrors)

    $Response = New-RMVMBasedDifferentialProfile @UserInput
    $ShouldExit = Start-RMDifferentialMigrationPreflight -DifferentialProfileId $Response.id `
        -IgnoreValidationErrors $IgnoreValidationErrors -RMMigrationReturn $RMMigrationReturn
    if ($ShouldExit) {
        return $RMMigrationReturn
    }

    $IsScheduled = ![string]::IsNullOrWhiteSpace($ScheduledAt)
    $MigrationResponse = Invoke-RMDifferentialMigrationPost -DifferentialProfileResponse $Response
    return Update-RMMigrationReturnAsSuccess -MigrationResponse $MigrationResponse `
        -RMMigrationReturn $RMMigrationReturn -IsScheduledMigration $IsScheduled `
        -ReturnMessage "Differential migration started successfully, migration ID"
}