title: Issue to create a dynamic Powershell Form, showing output data from the running the script
body: <p>I have a question about a Powershell Form script that I can't quite get to work yet.</p>
  <p>The code below does give the output I expect, but I can't manage to display the Form as it was during the initialization of 'establishing the connection'. The point is that we only want to start a program when there is connectivity with the router and the end user should also be able to see it that way, not just the end result.</p>
  <p>Can anyone point me in the right direction to make the form dynamically show what's happening at the time when the script is running?</p>
  <p>Ow, and also the 'Retry' (<code>[System.Windows.Forms.DialogResult]::Retry</code>)function does not do anything as expected... :(</p>
  <pre><code>#---------------------------------------------------------[Initialisations]--------------------------------------------------------
  # Init PowerShell Gui
  Add-Type -AssemblyName System.Windows.Forms
  Add-Type -AssemblyName System.Drawing
  
  #---------------------------------------------------------[Form]--------------------------------------------------------
  
  [System.Windows.Forms.Application]::EnableVisualStyles()
  
  $LocalRCForm                = New-Object system.Windows.Forms.Form
  $LocalRCForm.ClientSize     = '480,300'
  $LocalRCForm.text           = &quot;Status router connection&quot;
  $LocalRCForm.BackColor      = &quot;#F9F9F9&quot;
  $LocalRCForm.StartPosition  = &quot;CenterScreen&quot;
  $LocalRCForm.TopMost        = $true
  $LocalRCForm.SuspendLayout()
  
  $Titel                      = New-Object system.Windows.Forms.Label
  $Titel.text                 = &quot;Status router connection&quot;
  $Titel.AutoSize             = $true
  $Titel.width                = 25
  $Titel.height               = 10
  $Titel.location             = New-Object System.Drawing.Point(20,20)
  $Titel.Font                 = 'Microsoft Sans Serif,14,style=Bold'
  
  $Description                = New-Object system.Windows.Forms.Label
  $Description.text           = &quot;To start Security Desk, wait for an established connection&quot;
  $Description.AutoSize       = $false
  $Description.width          = 450
  $Description.height         = 50
  $Description.location       = New-Object System.Drawing.Point(20,60)
  $Description.Font           = 'Microsoft Sans Serif,10'
  
  $RCStatus                   = New-Object system.Windows.Forms.Label
  $RCStatus.text              = &quot;Status:&quot;
  $RCStatus.AutoSize          = $true
  $RCStatus.width             = 25
  $RCStatus.height            = 10
  $RCStatus.location          = New-Object System.Drawing.Point(20,115)
  $RCStatus.Font              = 'Microsoft Sans Serif,10,style=Bold'
  
  $RCFound                    = New-Object system.Windows.Forms.Label
  $RCFound.text               = &quot;Connecting to router...&quot;
  $RCFound.AutoSize           = $true
  $RCFound.width              = 25
  $RCFound.height             = 10
  $RCFound.location           = New-Object System.Drawing.Point(100,115)
  $RCFound.Font               = 'Microsoft Sans Serif,10'
  
  $RCDetails                  = New-Object system.Windows.Forms.Label
  $RCDetails.text             = &quot;Router succesfully connected&quot;
  $RCDetails.AutoSize         = $true
  $RCDetails.width            = 25
  $RCDetails.height           = 10
  $RCDetails.location         = New-Object System.Drawing.Point(20,150)
  $RCDetails.Font             = 'Microsoft Sans Serif,10,style=Bold'
  
  $RCTestConnection           = New-Object system.Windows.Forms.Label
  $RCTestConnection.text      = &quot;Checking connection&quot;
  $RCTestConnection.AutoSize  =
  $RCTestConnection.width     = 25
  $RCTestConnection.height    = 10
  $RCTestConnection.location  = New-Object System.Drawing.Point(100,115)
  $RCTestConnection.Font      = 'Microsoft Sans Serif,10'
  
  $AddRCBtn                   = New-Object system.Windows.Forms.Button
  $AddRCBtn.BackColor         = &quot;#ff7b00&quot;
  $AddRCBtn.text              = &quot;Ok&quot;
  $AddRCBtn.width             = 150
  $AddRCBtn.height            = 30
  $AddRCBtn.location          = New-Object System.Drawing.Point(300,250)
  $AddRCBtn.Font              = 'Microsoft Sans Serif,10'
  $AddRCBtn.ForeColor         = &quot;#000&quot;
  $AddRCBtn.DialogResult      = [System.Windows.Forms.DialogResult]::Yes
  
  $CloseBtn                       = New-Object system.Windows.Forms.Button
  $CloseBtn.BackColor             = &quot;#ffffff&quot;
  $CloseBtn.text                  = &quot;Close&quot;
  $CloseBtn.width                 = 90
  $CloseBtn.height                = 30
  $CloseBtn.location              = New-Object System.Drawing.Point(300,250)
  $CloseBtn.Font                  = 'Microsoft Sans Serif,10'
  $CloseBtn.ForeColor             = &quot;#000&quot;
  $CloseBtn.DialogResult          = [System.Windows.Forms.DialogResult]::Retry
  
  $LocalRCForm.controls.AddRange(@($Titel,$Description,$RCStatus,$RCFound,$RCType,$AddRCBtn,$CloseBtn,$RCDetails,$routerIp,$testConnection))
  
  #-----------------------------------------------------------[Functions]------------------------------------------------------------
  
  function AddRC {
    $RCFound.ForeColor = &quot;#000000&quot;
    $RCFound.Text = 'Connected to Router'
    }
  
  #---------------------------------------------------------[Script]--------------------------------------------------------
  # Check if router is online
  $routerIp = '1.1.1.1'
  $testConnection = Test-Connection $routerIp
  
  If ($testConnection) {
    $RCFound.text = &quot;Router Ready&quot;
    $RCFound.ForeColor = &quot;#7ed321&quot;
    $AddRCBtn.text = &quot;Run the program&quot;
    $CloseBtn.Visible = $false
    $RCDetails.Visible = $true
  }else{
    $RCFound.text = &quot;Router not found&quot;
    $RCFound.ForeColor = &quot;#D0021B&quot;
    $CloseBtn.text = &quot;Close&quot;
    $AddRCBtn.Visible = $false
    $RCDetails.Visible = $false
  }
  
  $AddRCBtn.Add_Click({ AddRC })
  
  $LocalRCForm.Add_Shown({$testConnection.Enabled = $true; $testConnection.Start()})
  
  return $LocalRCForm.ShowDialog()
  </code></pre>