Home > front end >  Issue to create a dynamic Powershell Form, showing output data from the running the script
Issue to create a dynamic Powershell Form, showing output data from the running the script

Time:01-24

I have a question about a Powershell Form script that I can't quite get to work yet.

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.

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?

Ow, and also the 'Retry' ([System.Windows.Forms.DialogResult]::Retry)function does not do anything as expected... :(

#---------------------------------------------------------[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           = "Status router connection"
$LocalRCForm.BackColor      = "#F9F9F9"
$LocalRCForm.StartPosition  = "CenterScreen"
$LocalRCForm.TopMost        = $true
$LocalRCForm.SuspendLayout()

$Titel                      = New-Object system.Windows.Forms.Label
$Titel.text                 = "Status router connection"
$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           = "To start Security Desk, wait for an established connection"
$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              = "Status:"
$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               = "Connecting to router..."
$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             = "Router succesfully connected"
$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      = "Checking connection"
$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         = "#ff7b00"
$AddRCBtn.text              = "Ok"
$AddRCBtn.width             = 150
$AddRCBtn.height            = 30
$AddRCBtn.location          = New-Object System.Drawing.Point(300,250)
$AddRCBtn.Font              = 'Microsoft Sans Serif,10'
$AddRCBtn.ForeColor         = "#000"
$AddRCBtn.DialogResult      = [System.Windows.Forms.DialogResult]::Yes

$CloseBtn                       = New-Object system.Windows.Forms.Button
$CloseBtn.BackColor             = "#ffffff"
$CloseBtn.text                  = "Close"
$CloseBtn.width                 = 90
$CloseBtn.height                = 30
$CloseBtn.location              = New-Object System.Drawing.Point(300,250)
$CloseBtn.Font                  = 'Microsoft Sans Serif,10'
$CloseBtn.ForeColor             = "#000"
$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 = "#000000"
  $RCFound.Text = 'Connected to Router'
  }

#---------------------------------------------------------[Script]--------------------------------------------------------
# Check if router is online
$routerIp = '1.1.1.1'
$testConnection = Test-Connection $routerIp

If ($testConnection) {
  $RCFound.text = "Router Ready"
  $RCFound.ForeColor = "#7ed321"
  $AddRCBtn.text = "Run the program"
  $CloseBtn.Visible = $false
  $RCDetails.Visible = $true
}else{
  $RCFound.text = "Router not found"
  $RCFound.ForeColor = "#D0021B"
  $CloseBtn.text = "Close"
  $AddRCBtn.Visible = $false
  $RCDetails.Visible = $false
}

$AddRCBtn.Add_Click({ AddRC })

$LocalRCForm.Add_Shown({$testConnection.Enabled = $true; $testConnection.Start()})

return $LocalRCForm.ShowDialog()

CodePudding user response:

There is too much to say about your script so let's focus on your main issue:

Your script will only show the form once the connection is tested, so the form will never display the various steps. To achieve your goal, you should put your main code in a function and run it once the form is displayed.

function funTestConnection {
    $testConnection = Test-Connection $routerIp
    If($testConnection) {
        $RCFound.text = "Router Ready"
        $RCFound.ForeColor = "#7ed321"
        $AddRCBtn.text = "Run the program"
        $CloseBtn.Visible = $false
        $RCDetails.Visible = $true
    } else {
        $RCFound.text = "Router not found"
        $RCFound.ForeColor = "#D0021B"
        $CloseBtn.text = "Close"
        $AddRCBtn.Visible = $false
        $RCDetails.Visible = $false
    }
}

Also, you are trying to add a control in your form that is not a control ($testConnection). I suppose you wanted to add $RCTestConnection instead (and don't forget to take care of it's position).

# Change this line:
$LocalRCForm.controls.AddRange(@($Titel,$Description,$RCStatus,$RCFound,$RCType,$AddRCBtn,$CloseBtn,$RCDetails,$routerIp,$testConnection))

# To this line:
$LocalRCForm.controls.AddRange(@($Titel,$Description,$RCStatus,$RCFound,$RCType,$AddRCBtn,$CloseBtn,$RCDetails,$routerIp,$RCTestConnection))

Again, you use the variable $testConnection but it neither have a property enabled or a method Start(). So this line makes no sense and after having put your main code in a function, it should be replaced as follow:

# Replace this line that has no meaning
$LocalRCForm.Add_Shown({$testConnection.Enabled = $true; $testConnection.Start()})

# By this line
$LocalRCForm.Add_Shown({funTestConnection})

About retrying if not connected, [System.Windows.Forms.DialogResult]::Retry is not a function, it's a constant value so you can't expect it to do anything. For it to be taken into account, you should use it in a way or the other.

One example would be like this:

# This code
do { $returnDialog = $LocalRCForm.ShowDialog() }
while($returnDialog -eq [System.Windows.Forms.DialogResult]::Retry)

# instead of this line
return $LocalRCForm.ShowDialog()

Finally, your function AddRC has no real impact as the form is closed when it's called (just a remark).

With all that said, you will have a functional script but it needs quite some adjustments related to what messages and information are displayed during the process (button names, labels, etc.) but at least it should do what you're expecting.

  •  Tags:  
  • Related