controls/script-run-modal.ps1

function New-UDScriptRunModal {
    param (
        [Parameter(Mandatory = $true)]
        $Script
    )
    
    #Todo - Change how the default is established.
    $DefaultPowerShellVersion = (Get-UAPowerShellVersion -Id 1).Version
    $RequiredPowerShellVersion = $Script.RequiredPowerShellVersion
    $RequiredPowerShellVersionDisplayString = if($null -eq $RequiredPowerShellVersion){"None Specified"}else{$RequiredPowerShellVersion}
    $PowerShellVersions = Get-UAPowerShellVersion
    $ScriptParameters = Get-UAScriptParameter -ScriptId $Script.Id
    
    Show-UDModal -Content {

            New-UDPageHeader -Text ("Manual Script Execution: $($Script.Name)")
            New-UDParagraph -Text "The Script will be executed immediately"
            #New-UDParagraph -Text " " #Spacer
            New-UDTabContainer -Tabs {
                New-UDTab -Text 'General' -Content {
                    New-UDCard -Content {
                        New-UDLayout -Columns 2 -Content {
                            New-UDHtml -Markup ("<b>PowerShell Version</b>: $($RequiredPowerShellVersionDisplayString)")
                            #New-UDHtml -Markup ("<b>Arguments</b>: ") #TODO Show Arugements
                        }
                    }
                    
                }

                if ($ScriptParameters.Length -gt 0)
                {
                    New-UDTab -Text 'Parameters' -Content {
                        New-UDCard -Content {
                            $ScriptParameters | ForEach-Object {
                                New-UDTextbox -Label $_.Name -Id ("RunScriptModal_" + $_.Name) -Type text
                            }
                        }
                           
                       #New-UDTextbox -Id "RunScriptModalTextBoxArgumentOverride" -Label "Arguments" -Type text
                    }

                }
                
                if($null -eq $RequiredPowerShellVersion)
                {

                    New-UDTab -Text 'Overrides' -Content {
                        New-UDCard -Content {
                            New-UDSelect -Id "RunScriptModalSelectPowerShellVersion" -Label "Override PowerShell Version" -Option {
                                New-UDSelectOption -Name "Use Default" -Value $DefaultPowerShellVersion
                                $PowerShellVersions | ForEach-Object {
                                    New-UDSelectOption -Name $_.Version -Value $_.Version
                                }
                            }
                        }
                           
                       #New-UDTextbox -Id "RunScriptModalTextBoxArgumentOverride" -Label "Arguments" -Type text
                    }
                }

                

            }

            New-UDElement -Tag div -Attributes @{
                style = @{
                    textAlign = "right" 
                }
            } -Content {
            
                New-UDButton -Text "Confirm" -Icon play -OnClick {

                    $RequiredPowerShellVersion = $Script.RequiredPowerShellVersion
                    $ScriptParameters = Get-UAScriptParameter -ScriptId $Script.Id
                    
                    if ($ScriptParameters.Length -gt 0)
                    {
                        $parameters = @{}
                        $ScriptParameters | ForEach-Object {
                            $TextBoxID = ("RunScriptModal_" + $_.Name)
                            #$parameters[$_.Name] = Get-Variable -Name $_.Name -ValueOnly
                            $parameters[$_.Name] = ((Get-UDElement -Id $TextBoxID).Attributes["value"])
                        }
                    }
                    
                    If($null -eq $RequiredPowerShellVersion)
                    {
                        $OverridePowerShellVersion = ((Get-UDElement -Id 'RunScriptModalSelectPowerShellVersion').Attributes["value"])
                        if($null -eq $OverridePowerShellVersion -or $OverridePowerShellVersion -eq "" -or $OverridePowerShellVersion -eq "null"){$OverridePowerShellVersion = $DefaultPowerShellVersion}
                        
                        #TODO $OverrideArguments = ((Get-UDElement -Id 'RunScriptModalTextBoxArgumentOverride').Attributes["value"])
                        
                        if ($ScriptParameters.Length -gt 0)
                        {
                            #TODO This Failes
                            $Job = Invoke-UAScript -Id $Script.Id -PowerShellVersion $OverridePowerShellVersion @parameters
                        }
                        else {
                            $Job = Invoke-UAScript -Id $Script.Id -PowerShellVersion $OverridePowerShellVersion
                        }
                    }
                    else
                    {
                        if ($ScriptParameters.Length -gt 0)
                        {
                            $Job = Invoke-UAScript -Id $Script.Id @parameters
                        }
                        else {
                            $Job = Invoke-UAScript -Id $Script.Id #TODO Argument Overrides
                        }
                        
                    }

                    #Sync the Various Grids
                    Sync-UDElement -Id "ScriptsGrid" -Broadcast
                    Sync-UDElement -Id "JobGrid" -Broadcast
                    Hide-UDModal
                }
            }
    }

}