Page 1 of 2

Passing parameters to exe

Posted: Thu Oct 22, 2015 1:10 pm
by fergusfog
I've read the article at https://www.sapien.com/blog/2012/10/25/revisiting-the-packager-and-command-line-arguments/ and while I'm far from being a PS expert I reckoned it wasn't that hard to set up a test script to Write-Host each of 2 params I passed to it.

So I coped the script, added the lines:
Write-Host $Commandline
Write-Host $Commandline[0]
Write-Host $Commandline[1]

and ran the packaged script with the line
script.exe param1 param2

Clearly this wasn't right because while $commandline returns
"param1" "param2"
the next lines gave an error.
I thought it was a string that gets split up by the code above..?

Can someone tell me my obvious error, and perhaps how I should modify this to properly work with 2 params?

Many thanks,

F.

Passing parameters to exe

Posted: Thu Oct 22, 2015 1:10 pm
by SAPIEN Support Forums
This is an automated post. A real person will respond soon.

Thank you for posting, fergusfog.

Here are some hints to help you get an accurate and complete answer to your question.

Ask in the best forum: If you asked in the wrong forum, just copy your question to the right forum.

Anticipate follow-up questions!

Did you remember to include the following?
  • 1. Product, version and build
    2. 32 or 64 bit product
    3. Operating system, e.g. Windows 7 64 bit.
    4. Attach a screenshot, if applicable
    5. Attach logs, crash reports, etc., in a ZIP file
If not, please take a moment to edit your original post or reply to this one.

*** Make sure you do not post any licensing information ***

Re: Passing parameters to exe

Posted: Thu Oct 22, 2015 1:19 pm
by jvierra
$commandline | %{Write-Host $_ -fore green}

Re: Passing parameters to exe

Posted: Thu Oct 22, 2015 2:50 pm
by fergusfog
That outputs exactly the same as

Write-Host $commandline

...except in green.

Re: Passing parameters to exe

Posted: Thu Oct 22, 2015 7:46 pm
by Alexander Riedel
https://www.sapien.com/blog/2010/02/09/primalforms-2009-the-packager-and-command-line-arguments/

Look at the packager commandline snippet.

Re: Passing parameters to exe

Posted: Thu Oct 22, 2015 9:49 pm
by jvierra
fergusfog wrote:That outputs exactly the same as

Write-Host $commandline

...except in green.
What is it that you were expecting?

Re: Passing parameters to exe

Posted: Thu Oct 22, 2015 10:35 pm
by Alexander Riedel
James, he wasn't asking about coloring or output. He asked about handling parameters as individual strings or objects. :D

Re: Passing parameters to exe

Posted: Fri Oct 23, 2015 12:33 am
by fergusfog
Alexander Riedel wrote:https://www.sapien.com/blog/2010/02/09/primalforms-2009-the-packager-and-command-line-arguments/

Look at the packager commandline snippet.
Do you mean this bit Alex..?
Results:
$Parameter[0] = –Parameter
$Parameter[1] = Value1

$Parameter is never mentioned in the script above it, and calling these array members returns null.

As does trying to return $Arguments[0], which I would have thought is the one containing the array of args..?

How do I return param1 and param2 individually?

F.

Re: Passing parameters to exe

Posted: Fri Oct 23, 2015 1:17 am
by jvierra
THe variable $CommandLine is just a string.

Processing $CommandLine creates a dictionary of name/value pairs if you have passed named parameters.

app.exe -MyParm p1 -MyParm2 p2

This gives us:
PowerShell Code
Double-click the code block to select all.
Key       Value
--------  ----------
MyParam   p1
MyParam2  p2
There is no $Parameter array.

Re: Passing parameters to exe

Posted: Fri Oct 23, 2015 1:28 am
by jvierra
You need to read this article and read it carefully:

https://www.sapien.com/blog/2010/02/09/ ... arguments/

This is how to get the dictionary:
PowerShell Code
Double-click the code block to select all.
$myargs=Parse-Commandline $CommandLine 
#Convert the Arguments. Use – as the Argument Indicator 
$named=Convert-ArgumentsToHashtable $myargs ‘-‘
Now you get your arguments by name from $dictionary by name

$myparam=$named['myparam']