PowerShell Form (GUI) Unexpected behavior

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
dst_cncs
Posts: 3
Joined: Mon Nov 09, 2020 7:09 am

PowerShell Form (GUI) Unexpected behavior

Post by dst_cncs »

Product, version and build: PowerShell Studio 2020 Version 5.7.182
Operating system: Windows Server 2016
PowerShell version(s): 5.1.14393.3866

Hello,

I'm building a PowerShell Form to detect and connect to our VPN when needed. I have various conditions (checking network interfaces, domain connectivity, etc) and I'm catching errors from rasdial. Everything is working as expected. There is no user interaction. It's jus ta GUI to let the user know what's going on.

On one of the conditions, I have a for loop, that retries the connection 10 times. At the 10th time it outputs that the connection failed and the user should contact the administrator (with an if($i -eq 0)). On all conditions I'm using:

$formtext.Close()
return

But on this case the script just goes to the first line and starts again. I might be doing something wrong with the GUI. I have the form load empty:
  1. $form_Load = {
  2.  
  3.  
  4.  
  5. }
  6.  
  7.  
  8.  
  9. And created my script like:
  10.  
  11.  
  12.  
  13. $myscript = {
  14.  
  15.     (...) my code (...)
  16.  
  17.  
  18.  
  19. foreach($adapter in $netProfile)
  20.  
  21. { (...) }
  22.  
  23.  
  24.  
  25.     #if anything was not taken in account, close the form anyway
  26.  
  27.     #there is no x button or icon on taskbar
  28.  
  29.     #can be forced to close on Task Manager
  30.  
  31.     escreverEventLog -eventID 999 -message "Fechar script (form) caso chegue ao final do script sem que nenhuma das condições anteriores o tenha fechado"
  32.  
  33.     $formAlwaysOnVPNRedeCorp.Close()
  34.  
  35.     return 0
  36.  
  37. }
That for loop is inside the foreach. As I'm checking interfaces up, in reality I only have one item to iterate.
On the properties, on the right menu, under Load I have the formLoad, and on Active I have myScript. At the end of this event I have the the code above, writing to the event viewer but this part never runs.

As this is automatic, with no buttons, or user interaction, am I doing something wrong regarding the way I should use the form? Just want to stop the script after the foreach and close the form, not running anything elese.
Thank you.

jvierra
Posts: 14672
Joined: Tue May 22, 2007 9:57 am
Answers: 6
Has voted: 1 time
Been upvoted: 5 times
Contact:

Re: PowerShell Form (GUI) Unexpected behavior

Post by jvierra »

The information you have given is too incomplete. It is not possible to understand what is happening or why.

Locked