Private/Show-MigrationForm.ps1

function Show-MigrationForm {
    # Load configuration
    $Config = Read-Config

    # Extract configuration values
    $AppName      = $Config["Variables"]["AppName"]
    $IconBase64   = $Config["Images"]["MainIcon"]
    $ImagePath    = $Config["Images"]["MainImage"]
    $NextPath     = $Config["Images"]["SecondaryImage"]
    $rightImage   = $Config["Images"]["TickImage"]
    $crossImage   = $Config["Images"]["CrossImage"]
    $Domain1      = $Config["Domain"]["Domain1"]
    $Domain2      = $Config["Domain"]["Domain2"]
    $FileServer1  = $Config["FileShares"]["FileserverTest"]
    $FileServer2  = $Config["FileShares"]["FileServerProd"]

    # Initialize form
    [void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
    [System.Windows.Forms.Application]::EnableVisualStyles()

    $LogFilePath = Initialize-Log
    Write-Log -Message "Log file initialized at: $LogFilePath" -LogFilePath $LogFilePath


    $form2 = New-Object System.Windows.Forms.Form
    $form2.Text = $AppName
    $form2.Size = New-Object System.Drawing.Size(600, 400)
    $form2.StartPosition = "CenterScreen"
    $form2.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedDialog
    $form2.MaximizeBox = $false
    if (Test-Path $IconBase64) {
        $form2.Icon = New-Object System.Drawing.Icon($IconBase64)
    }


    # Add left-side image
    $pictureBox = New-Object System.Windows.Forms.PictureBox
    $pictureBox.ImageLocation = $ImagePath
    $pictureBox.SizeMode = [System.Windows.Forms.PictureBoxSizeMode]::StretchImage
    $pictureBox.Size = New-Object System.Drawing.Size(200, 400)
    $pictureBox.Location = New-Object System.Drawing.Point(0, 0)
    $form2.Controls.Add($pictureBox)

    # Add panel for controls
    $panel = New-Object System.Windows.Forms.Panel
    $panel.Location = New-Object System.Drawing.Point(220, 10)
    $panel.Size = New-Object System.Drawing.Size(360, 350)
    $form2.Controls.Add($panel)

    # Create PictureBox inside the panel for the icon
    $iconBox = New-Object System.Windows.Forms.PictureBox
    $iconBox.Size = New-Object System.Drawing.Size(20, 20)
    $iconBox.Location = New-Object System.Drawing.Point(320, 35) # Position next to the TextBox
    $iconBox.SizeMode = [System.Windows.Forms.PictureBoxSizeMode]::StretchImage
    $panel.Controls.Add($iconBox)

    # Email input
    $labelEmail = New-Object System.Windows.Forms.Label
    $labelEmail.Text = "Enter your email address:"
    $labelEmail.Location = New-Object System.Drawing.Point(10, 10)
    $labelEmail.Size = New-Object System.Drawing.Size(200, 20)
    $panel.Controls.Add($labelEmail)

    $textBoxEmail = New-Object System.Windows.Forms.TextBox
    $textBoxEmail.Location = New-Object System.Drawing.Point(10, 35)
    $textBoxEmail.Size = New-Object System.Drawing.Size(300, 20)
    $panel.Controls.Add($textBoxEmail)

    # Migration dropdown
    $labelMigrateProfile = New-Object System.Windows.Forms.Label
    $labelMigrateProfile.Text = "Do you want your profile to be migrated?"
    $labelMigrateProfile.Location = New-Object System.Drawing.Point(10, 70)
    $labelMigrateProfile.Size = New-Object System.Drawing.Size(300, 20)
    $labelMigrateProfile.Visible = $false
    $panel.Controls.Add($labelMigrateProfile)

    $comboBoxMigrateProfile = New-Object System.Windows.Forms.ComboBox
    $comboBoxMigrateProfile.Items.AddRange(@("Yes", "No"))
    $comboBoxMigrateProfile.Location = New-Object System.Drawing.Point(10, 95)
    $comboBoxMigrateProfile.Size = New-Object System.Drawing.Size(300, 20)
    $comboBoxMigrateProfile.DropDownStyle = "DropDownList"
    $comboBoxMigrateProfile.Visible = $false
    $panel.Controls.Add($comboBoxMigrateProfile)

    # VDI profile selection
    $labelVDIProfile = New-Object System.Windows.Forms.Label
    $labelVDIProfile.Text = "Select the VDI Profile you want to migrate:"
    $labelVDIProfile.Location = New-Object System.Drawing.Point(10, 130)
    $labelVDIProfile.Size = New-Object System.Drawing.Size(300, 20)
    $labelVDIProfile.Visible = $false
    $panel.Controls.Add($labelVDIProfile)

    $radioTest = New-Object System.Windows.Forms.RadioButton
    $radioTest.Text = "Test"
    $radioTest.Location = New-Object System.Drawing.Point(10, 155)
    $radioTest.Size = New-Object System.Drawing.Size(50, 20)
    $radioTest.Visible = $false
    $panel.Controls.Add($radioTest)

    $radioProd = New-Object System.Windows.Forms.RadioButton
    $radioProd.Text = "Prod"
    $radioProd.Location = New-Object System.Drawing.Point(70, 155)
    $radioProd.Size = New-Object System.Drawing.Size(50, 20)
    $radioProd.Visible = $false
    $panel.Controls.Add($radioProd)

    # Data migration options
    $labelDataMigration = New-Object System.Windows.Forms.Label
    $labelDataMigration.Text = "Select the Data you want to migrate:"
    $labelDataMigration.Location = New-Object System.Drawing.Point(10, 190)
    $labelDataMigration.Size = New-Object System.Drawing.Size(300, 20)
    $labelDataMigration.Visible = $false
    $panel.Controls.Add($labelDataMigration)

    $checkDesktop = New-Object System.Windows.Forms.CheckBox
    $checkDesktop.Text = "Desktop"
    $checkDesktop.Location = New-Object System.Drawing.Point(10, 215)
    $checkDesktop.Size = New-Object System.Drawing.Size(100, 20)
    $checkDesktop.Visible = $false
    $panel.Controls.Add($checkDesktop)

    $checkDocuments = New-Object System.Windows.Forms.CheckBox
    $checkDocuments.Text = "Documents"
    $checkDocuments.Location = New-Object System.Drawing.Point(120, 215)
    $checkDocuments.Size = New-Object System.Drawing.Size(100, 20)
    $checkDocuments.Visible = $false
    $panel.Controls.Add($checkDocuments)

    $checkDownloads = New-Object System.Windows.Forms.CheckBox
    $checkDownloads.Text = "Downloads"
    $checkDownloads.Location = New-Object System.Drawing.Point(10, 240)
    $checkDownloads.Size = New-Object System.Drawing.Size(100, 20)
    $checkDownloads.Visible = $false
    $panel.Controls.Add($checkDownloads)

    $checkCompleteProfile = New-Object System.Windows.Forms.CheckBox
    $checkCompleteProfile.Text = "Complete Profile"
    $checkCompleteProfile.Location = New-Object System.Drawing.Point(120, 240)
    $checkCompleteProfile.Size = New-Object System.Drawing.Size(150, 20)
    $checkCompleteProfile.Visible = $false
    $panel.Controls.Add($checkCompleteProfile)

    # Add buttons
    $buttonMigrate = New-Object System.Windows.Forms.Button
    $buttonMigrate.Text = "Migrate Now"
    $buttonMigrate.Location = New-Object System.Drawing.Point(10, 275)
    $buttonMigrate.Size = New-Object System.Drawing.Size(200, 30)
    $buttonMigrate.BackColor = [System.Drawing.Color]::SteelBlue
    $buttonMigrate.ForeColor = [System.Drawing.Color]::White
    $buttonMigrate.Font = New-Object System.Drawing.Font("Arial", 10, [System.Drawing.FontStyle]::Bold)
    $buttonMigrate.Visible = $false
    $panel.Controls.Add($buttonMigrate)

    $buttonFinish = New-Object System.Windows.Forms.Button
    $buttonFinish.Location = New-Object System.Drawing.Point(320, 310)
    $buttonFinish.Size = New-Object System.Drawing.Size(40, 40)
    $buttonFinish.BackColor = [System.Drawing.Color]::SteelBlue
    $buttonFinish.ForeColor = [System.Drawing.Color]::White
    $buttonFinish.Visible = $false
    if (Test-Path $NextPath) {
        $icon = [System.Drawing.Image]::FromFile($NextPath)
        $resizedIcon = New-Object System.Drawing.Bitmap($icon, 40, 40)
        $buttonFinish.Image = $resizedIcon
        $buttonFinish.ImageAlign = [System.Drawing.ContentAlignment]::MiddleCenter
    }
    $buttonFinish.FlatAppearance.BorderSize = 0 
    $panel.Controls.Add($buttonFinish)

    # Add progress bar
    $progressBar = New-Object System.Windows.Forms.ProgressBar
    $progressBar.Location = New-Object System.Drawing.Point(10, 320)
    $progressBar.Size = New-Object System.Drawing.Size(300, 25)
    $progressBar.Minimum = 0
    $progressBar.Maximum = 100
    $progressBar.Value = 0
    $progressBar.Visible = $false
    $panel.Controls.Add($progressBar)

    #Migrate button click event
    $buttonMigrate.Add_Click({
        $buttonMigrate.Enabled = $false
        $progressBar.Value = 0

        # Retrieve the source username from email
        $sAMAccountName = Get-UserSamAccountNameByEmail -EmailAddress $($textBoxEmail.Text)
    
        if ($checkCompleteProfile.Checked) {
            # Full Profile Migration
            $progressBar.Visible = $true
            $progressBar.Maximum = 100
            Start-Migration -SourceUsername $sAMAccountName -DestinationUsername $env:USERNAME -SharePath $networkpathvariable -LogFilePath $LogFilePath -DryRun
            $progressBar.Value = 100
            $buttonFinish.Visible = $true
        } else {
            # Partial Migration for selected folders
            $progressBar.Visible = $true
            $foldersToMigrate = @()
            if ($checkDesktop.Checked) { $foldersToMigrate += "Desktop" }
            if ($checkDocuments.Checked) { $foldersToMigrate += "Documents" }
            if ($checkDownloads.Checked) { $foldersToMigrate += "Downloads" }
    
            if ($foldersToMigrate.Count -gt 0) {
                $progressBar.Maximum = $foldersToMigrate.Count * 10
                $progressBar.Value = 0
    
                foreach ($folder in $foldersToMigrate) {
                    Start-Migration -SourceUsername $sAMAccountName -DestinationUsername $env:USERNAME -SharePath $networkpathvariable -LogFilePath $LogFilePath -Folder $folder -DryRun
                    $progressBar.Value += 10
                    Start-Sleep -Milliseconds 500
                }
    
                $progressBar.Value = $progressBar.Maximum
                $buttonFinish.Visible = $true
            } else {
                [System.Windows.Forms.MessageBox]::Show("Please select either 'Full Profile' or individual folders to migrate.", "No Migration Selected")
            }
        }
    
        $buttonMigrate.Enabled = $true
     })

     #ComboBox SelectedIndexChanged Event
     $comboBoxMigrateProfile.add_SelectedIndexChanged({
         if ($comboBoxMigrateProfile.SelectedItem -eq "Yes") {
             $labelVDIProfile.Visible = $true
             $radioTest.Visible = $true
             $radioProd.Visible = $true
             $labelDataMigration.Visible = $true
             $checkDesktop.Visible = $true
             $checkDocuments.Visible = $true
             $checkDownloads.Visible = $true
             $checkCompleteProfile.Visible = $true
             $buttonMigrate.Visible = $true
         } elseif ($comboBoxMigrateProfile.SelectedItem -eq "No") {
             [System.Windows.Forms.MessageBox]::Show("Migration Cancelled")
             $form2.Close()
         }
     })
     
     #Textbox text changeevent
     $textBoxEmail.Add_TextChanged({
         $email = $textBoxEmail.Text.Trim()
     
         if (-not [string]::IsNullOrWhiteSpace($email)) {
             if ($email -like "*$Domain1" -or $email -like "*$Domain2") {
                 $isValid = Get-UserSamAccountNameByEmail -EmailAddress $email
                 
                 if ($isValid -ne $null) {
                     Write-Log -Message "The SamAccountname of the user with email Adrress $email : $isValid" -LogFilePath $LogFilePath
                     $iconBox.ImageLocation = $rightImage
                     $labelMigrateProfile.Visible = $true
                     $comboBoxMigrateProfile.Visible = $true
                     $textBoxEmail.ReadOnly = $true
                 } else {
                     Write-Log -Message "The Email Adrress $email entered is incorrect" -LogFilePath $LogFilePath
                     $iconBox.ImageLocation = $crossImage
                     $labelMigrateProfile.Visible = $false
                     $comboBoxMigrateProfile.Visible = $false
                 }
             } else {
                 $iconBox.ImageLocation = $crossImage
                 $labelMigrateProfile.Visible = $false
                 $comboBoxMigrateProfile.Visible = $false
             }
         } else {
             $iconBox.Image = $null
             $labelMigrateProfile.Visible = $false
             $comboBoxMigrateProfile.Visible = $false
         }
     })

     #Fullprofile Migration Selecet Event
     $checkCompleteProfile.Add_CheckedChanged({
         if ($checkCompleteProfile.Checked) {
             $checkDesktop.Checked = $false
             $checkDocuments.Checked = $false
             $checkDownloads.Checked = $false
         }
     })

    #Individual Folders Migration Select event
    $checkDesktop.Add_CheckedChanged({
        if ($checkDesktop.Checked) {
            $checkCompleteProfile.Checked = $false
        }
    })
    
    $checkDocuments.Add_CheckedChanged({
        if ($checkDocuments.Checked) {
            $checkCompleteProfile.Checked = $false
        }
    })
    
    $checkDownloads.Add_CheckedChanged({
        if ($checkDownloads.Checked) {
            $checkCompleteProfile.Checked = $false
        }
    })

    #Radio button Check Change Event
    $radioTest.Add_CheckedChanged({
        if ($radioTest.Checked) {
            Set-Variable -Name networkpathvariable -Scope Global -Value $FileServer1
        }
    })
    
    $radioProd.Add_CheckedChanged({
        if ($radioProd.Checked) {
            Set-Variable -Name networkpathvariable -Scope Global -Value $FileServer2
        }
    })

    #Show Final Form
    $buttonFinish.Add_Click({
        $form2.Hide()
        Show-FinalForm
        $form2.Dispose()
    })


    # Show the form
    $form2.ShowDialog()
}