Command line parameters & Windows Forms in PSS

This forum can be browsed by the general public. Posting is limited to current SAPIEN license holders with active maintenance and does not offer a response time guarantee.
Forum rules
DO NOT POST LICENSE NUMBERS, ACTIVATION KEYS OR ANY OTHER LICENSING INFORMATION IN THIS FORUM.
Only the original author and our tech personnel can reply to a topic that is created in this forum. If you find a topic that relates to an issue you are having, please create a new topic and reference the other in your post.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 4 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
User avatar
FrankAndrew
Posts: 164
Last visit: Sat Apr 01, 2023 1:52 am

Command line parameters & Windows Forms in PSS

Post by FrankAndrew »

Product, version and build: PowerShell Studio 2019, 5.6.165
32 or 64 bit version of product: 64bit
Operating system: Windows 7 Enterprise & Windows 10 Enterprise (1809)
32 or 64 bit OS: 64bit

I have been programming with PrimalScript since 2002. (Just so that you know where I am coming from)

I have taken a deep dive into Windows Forms, in the last two months, using PowerShell Studio, now that I have a REAL BIG NEED for a GUI on top of a PowerShell Script.

I must complement all at SAPIEN for the GREAT tool for creating GUIs in PSS.
Also many thanks to June Blender for her very basic/generic straight forward videos in YouTube. They gave me the start that I needed to jump right in.

Some of the videos from LazyWinAdmin are also very helpful, thanks to those people also.

Now to my problem I have the need to pass some command line parameters to the script/form at start time, and then also through the EXE built from the deploy tool.

Where is the logic to be placed, exactly?

I put a normal PS1 style Param() section at the top of the PSF file, outside of ANY function definitions and that works when either hitting Run button or in Debug button in PSS.
BUT sometimes when I am trying to debug PSS DOES NOT STOP AT ANY of the Breakpoints that are set! (When in Debug mode of course)
It has done so before and then at other times it does NOT.

I read something in the Internet that you have some functions for passing and evaluating the command line Parameters, Convert-CommandLineToDictionary() and Parse-Commandline(), but it was NOT clear to me where exactly the code was to be placed.

I have looked at June Blenders URL:

Code: Select all

https://www.sapien.com/blog/2015/11/30/passing-parameters-to-a-script-in-an-executable-file/
But that does NOT cover PSF files. Are they different or should it acted the same?

Would you guys please give me some more detailed examples of exactly where these functions are supposed to be placed?

If the code outside of the function blocks should NOT be used or is unreliable why does PSS not warn me about that code?
User avatar
Alexander Riedel
Posts: 8479
Last visit: Thu Mar 28, 2024 9:29 am
Answers: 19
Been upvoted: 37 times

Re: Command line parameters & Windows Forms in PSS

Post by Alexander Riedel »

PSF files are basically similar to project files in Visual Studio, which I know you are familiar with. At the end, a .PS1 file is always generated. You can look at that by using the Export function on the ribbon.
The method of using $EngineArgs applies to anything generated out of PSF files as well. It is generally a better way than using the standard script Parameter declarations since packaged code is not executed as a script.
Different engines also have different threading architectures, so that is always the better way.
Where you place that code is largely dependent on your GUI architecture and your needs.
Personally I would use the Form’s creation handler to get and process command line arguments and store any information as needed further down the line.

As for not warning about code outside of function blocks, we do not know what your intentions are, Powershell has a pretty loose language specification, so anything is possible. Who are we to tell you where you cannot place code? :-)

As a general good practices rule, just as with virtually any other language, global code and nested functions have their place, which is why they exists. But for a clean design you usually should avoid them unless they are absolutely necessary.
Global variables in PowerShell are a necessity almost, so you won’t be able to avoid those. Again, we will not warn or discourage any of these, since we do not know what you want to accomplish.
Alexander Riedel
SAPIEN Technologies, Inc.
User avatar
FrankAndrew
Posts: 164
Last visit: Sat Apr 01, 2023 1:52 am

Re: Command line parameters & Windows Forms in PSS

Post by FrankAndrew »

Hi Alex,

Good to hear from you again, it's been a Long time, hope everything is going well.

I am NOT familiar with Visual Studio I have almost never used it. (Many years ago…(in a Galaxy...) well you get the idea)
So I don't know about the $EngineArgs.

I will try putting the params in the Form’s creation handler and see what happens...
User avatar
FrankAndrew
Posts: 164
Last visit: Sat Apr 01, 2023 1:52 am

Re: Command line parameters & Windows Forms in PSS

Post by FrankAndrew »

Hi Alex,

Well that did not get me anywhere.

Now I get the following error message:
Error message with Param() in $form...Load function
Error message with Param() in $form...Load function
2019.06.24_13-14-19_WindowsForm_CommandLineParams_Now_in_Form...Load_Function_ErrorStarting.png (28.42 KiB) Viewed 4790 times
I created at new very small sample to see what happens with three command line Parameters:

Here is the script Code:

Code: Select all

$formTestCommandLinePassing_Load={
   Param (
      [string]$parameter1,
      [string]$parameter2,
      [string]$parameter3
   )
   #
   $textboxParameter1.Text = $parameter1
   $textboxParameter2.Text = $parameter2
   $textboxParameter3.Text = $parameter3
}

$buttonExit_Click={
	$formTestCommandLinePassing.Close()
}
Here is a screenshot from the designer:
Designer View of test form
Designer View of test form
2019.06.24_13-48-26_WindowsForm_DesignerView_Simple_ParameterOutput.png (13.27 KiB) Viewed 4790 times
Here is the Dialog/form after hitting the Debug button in PSS:
(I was NOT prompted for any CommandLineParameters as I would be if they were at the top of this form outside of the form...Load function)
After hitting Debug button
After hitting Debug button
2019.06.24_13-50-12_WindowsForm_Display_After_Hitting_The_DebugButton_in_PSS.png (5.22 KiB) Viewed 4790 times
I hope you can see what I cannot see. Maybe you guys can create a small tutorial for passing command line Parameters to forms, when Debugging (or trying to), and when it is the "Deployed" as an EXE.

That would be a GREAT help.

Gruß,
Drew
User avatar
FrankAndrew
Posts: 164
Last visit: Sat Apr 01, 2023 1:52 am

Re: Command line parameters & Windows Forms in PSS

Post by FrankAndrew »

Hi Again Alex,

This is more what I was expecting to work:

Script Code:

Code: Select all

Param (
   [string]$parameter1 = "Test 1",
   [string]$parameter2 = "Test2",
   [string]$parameter3 = ""
)
#
$formTestCommandLinePassing_Load={
   $textboxParameter1.Text = $parameter1
   $textboxParameter2.Text = $parameter2
   $textboxParameter3.Text = $parameter3
}

$buttonExit_Click={
	$formTestCommandLinePassing.Close()
}
Designer shot:
Shot of the form in the Designer
Shot of the form in the Designer
2019.06.24_14-03-25_WindowsForm_DesignerShot_ParamsFromOutside.png (14 KiB) Viewed 4785 times
Shot of the Parameters prompt from PSS when running or Debugging:
Parameters Prompt from PSS
Parameters Prompt from PSS
2019.06.24_14-05-15_WindowsForm_Parameters_Prompt_From_PSS_after_Hitting_DebugButton.png (4.19 KiB) Viewed 4785 times
Shot of Dialog after Parameter confirmation:
Final Display of dialog
Final Display of dialog
2019.06.24_14-06-13_WindowsForm_Display_After_Parameter_Confirmation.png (4.52 KiB) Viewed 4785 times
That is how simple I thought it was supposed to be.
User avatar
Olga_B
Site Admin
Posts: 196
Last visit: Thu Mar 28, 2024 8:34 am

Re: Command line parameters & Windows Forms in PSS

Post by Olga_B »

Make parameters Mandatory, and you will see Parameters prompt

Param (
[Parameter(Mandatory = $true)]
[string]$parameter1,
[Parameter(Mandatory = $true)]
[string]$parameter2,
[Parameter(Mandatory = $true)]
[string]$parameter3
)
User avatar
FrankAndrew
Posts: 164
Last visit: Sat Apr 01, 2023 1:52 am

Re: Command line parameters & Windows Forms in PSS

Post by FrankAndrew »

Hi Olga_B,

I don't know where you want me to put those options.

I have two examples listed above.

In the bottom example where the Param() section is outside of the $form...Load function there is NO problem getting the Parameters prompting dialog to appear in PSS.

So I tried your suggestion in my first example where the Param() section IS IN the $form...Load function see code:

Code: Select all

$formTestCommandLinePassing_Load = {
   param
   (
      [Parameter(Mandatory = $true)]
      [string]$parameter1 = "Test 1",
      [Parameter(Mandatory = $true)]
      [string]$parameter2 = "Test2",
      [Parameter(Mandatory = $true)]
      [string]$parameter3 = ""
   )
   $textboxParameter1.Text = $parameter1
   $textboxParameter2.Text = $parameter2
   $textboxParameter3.Text = $parameter3
}

$buttonExit_Click={
   $formTestCommandLinePassing.Close()
}
But that does NOT help to get the Parameters prompting dialog to show up in PSS when I am trying to 'Test' run it.
User avatar
Olga_B
Site Admin
Posts: 196
Last visit: Thu Mar 28, 2024 8:34 am

Re: Command line parameters & Windows Forms in PSS

Post by Olga_B »

Param() section should be outside Load function
User avatar
FrankAndrew
Posts: 164
Last visit: Sat Apr 01, 2023 1:52 am

Re: Command line parameters & Windows Forms in PSS

Post by FrankAndrew »

I do NOT want the parameters to be mandatory because if they are a default value cannot be used.

And again there is NO problem with the Parameters prompt dialog showing up when the parameters are on the outside.

I am at home right now and using the PSS version 5.6.165 on Windows 7 Home Premium both 64 bit and the creation of an EXE file also worked just as expected, non mandatory parameters, with the defaults both in the debug mode and as an EXE file.
I will text exactly what is going on at work under Windows 10 (1809) 64 bit version.
Talk to you tomorrow...
This topic is 4 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.