depreciated/Start-OSDProgress.ps1

Function Start-OSDProgress {

    # Overload OSD Save-WebFile function
    Register-OSDOverride

    # Create runspace
    $syncHash = [hashtable]::Synchronized(@{})
    $newRunspace = [runspacefactory]::CreateRunspace()
    $syncHash.Runspace = $newRunspace
    $syncHash.IsRunning = $true
    $newRunspace.ApartmentState = "STA"
    $newRunspace.ThreadOptions = "ReuseThread"
    $newRunspace.Open() | Out-Null

    # Load unlock password
    $unlockPwd = Receive-Password

    # Add var to runspace
    $newRunspace.SessionStateProxy.SetVariable("syncHash", $syncHash)
    $newRunspace.SessionStateProxy.SetVariable("xamlFile", $xamlFile)
    $newRunspace.SessionStateProxy.SetVariable("UIPath", $UIPath)
    $newRunspace.SessionStateProxy.SetVariable("unlockPwd", $unlockPwd)

    $PowerShellCommand = [PowerShell]::Create().AddScript( {

            # Loads assembly
            [void][System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms")
            [void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')

            $AssemblyLocation = Join-Path -Path $UIPath -ChildPath "assembly"
            foreach ($Assembly in (Get-ChildItem $AssemblyLocation -Filter *.dll)) {
                [System.Reflection.Assembly]::LoadFrom($Assembly.fullName) | out-null
            }

            # Load MainWindow
            $XamlLoader = [System.Xml.XmlDocument]::new()
            $XamlLoader.Load($xamlFile)
            $XamlMainWindow = $XamlLoader

            $Reader = [System.Xml.XmlNodeReader]::new($XamlMainWindow)
            $syncHash.Form = [Windows.Markup.XamlReader]::Load($Reader)

            # Load Elements
            $syncHash.Setup_Badge = $syncHash.Form.FindName("Setup_Badge")
            $syncHash.Setup_Line = $syncHash.Form.FindName("Setup_Line")
            $syncHash.Download_Bade = $syncHash.Form.FindName("Download_Badge")
            $syncHash.Download_Line = $syncHash.Form.FindName("Download_Line")
            $syncHash.Computer_Bade = $syncHash.Form.FindName("Computer_Badge")
            $syncHash.Step_Status = $syncHash.Form.FindName("Step_Status")
            $syncHash.Progress = $syncHash.Form.FindName("Progress")
            $syncHash.Unlock = $syncHash.Form.FindName("Unlock")

            # blocking easy escape from screen
            # $MainWindow = $syncHash.Form.FindName("MainWindow")
            # $MainWindow.Add_MouseDoubleClick({
            # [System.Windows.MessageBox]::Show('MouseDoubleClick')
            # })
            # $MainWindow.Add_MouseLeftButtonDown({
            # [System.Windows.MessageBox]::Show('MouseLeftButtonDown')
            # })
            # $MainWindow.Add_MouseRightButtonDown({
            # [System.Windows.MessageBox]::Show('this will not happen')
            # })

            #################### Set start values ####################
            $syncHash.Setup_Line.Opacity = "0"
            $syncHash.Download_Line.Opacity = "0"
            $syncHash.Step_Status.Content = "Phase : Configuration"
            $syncHash.Phase = "Start"

            ################### unlock eventhandler ##################
            $syncHash.Unlock.Add_Click( {
                    $result = [MahApps.Metro.Controls.Dialogs.DialogManager]::ShowModalInputExternal($syncHash.Form, "", "Enter unlock password")

                    if ($result -eq $unlockPwd) {
                        $syncHash.Form.close()
                        $syncHash.IsRunning = $false
                    }
                })

            # close windows button
            $syncHash.Form.Add_Closing( {
                    #$_.Cancel = $true
                    $syncHash.IsRunning = $false
                    Stop-OSDProgressServer
                })

            # show form
            $syncHash.Form.ShowDialog() | out-null

            $syncHash.Error = $Error
        })
    $PowerShellCommand.Runspace = $newRunspace
    $PowerShellCommand.BeginInvoke() | Out-Null

    # cleanup eventhandler
    Register-ObjectEvent -InputObject $SyncHash.Runspace -EventName 'AvailabilityChanged' -Action {
        if ($Sender.RunspaceAvailability -eq "Available") {

            # remove overloaded OSD Save-WebFile function
            Unregister-OSDOverride
            # Remove-Item alias:Save-WebFile

            $Sender.Closeasync()
            $Sender.Dispose()
        }
    } | Out-Null

    $Script:ProgressUI = $syncHash
}