Passing parameters to exe

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
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.
User avatar
fergusfog
Posts: 13
Joined: Wed Jun 08, 2011 1:15 am

Passing parameters to exe

Post by fergusfog » Thu Oct 22, 2015 1:10 pm

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.

User avatar
SAPIEN Support Forums
Posts: 945
Joined: Wed Dec 03, 2014 2:26 pm

Passing parameters to exe

Post by SAPIEN Support Forums » Thu Oct 22, 2015 1:10 pm

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 ***

User avatar
jvierra
Posts: 13717
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Passing parameters to exe

Post by jvierra » Thu Oct 22, 2015 1:19 pm

$commandline | %{Write-Host $_ -fore green}

User avatar
fergusfog
Posts: 13
Joined: Wed Jun 08, 2011 1:15 am

Re: Passing parameters to exe

Post by fergusfog » Thu Oct 22, 2015 2:50 pm

That outputs exactly the same as

Write-Host $commandline

...except in green.

User avatar
Alexander Riedel
Posts: 7017
Joined: Tue May 29, 2007 4:43 pm

Re: Passing parameters to exe

Post by Alexander Riedel » Thu Oct 22, 2015 7:46 pm

https://www.sapien.com/blog/2010/02/09/primalforms-2009-the-packager-and-command-line-arguments/

Look at the packager commandline snippet.
Alexander Riedel
SAPIEN Technologies, Inc.

User avatar
jvierra
Posts: 13717
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Passing parameters to exe

Post by jvierra » Thu Oct 22, 2015 9:49 pm

fergusfog wrote:That outputs exactly the same as

Write-Host $commandline

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

User avatar
Alexander Riedel
Posts: 7017
Joined: Tue May 29, 2007 4:43 pm

Re: Passing parameters to exe

Post by Alexander Riedel » Thu Oct 22, 2015 10:35 pm

James, he wasn't asking about coloring or output. He asked about handling parameters as individual strings or objects. :D
Alexander Riedel
SAPIEN Technologies, Inc.

User avatar
fergusfog
Posts: 13
Joined: Wed Jun 08, 2011 1:15 am

Re: Passing parameters to exe

Post by fergusfog » Fri Oct 23, 2015 12:33 am

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.

User avatar
jvierra
Posts: 13717
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Passing parameters to exe

Post by jvierra » Fri Oct 23, 2015 1:17 am

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.

User avatar
jvierra
Posts: 13717
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Passing parameters to exe

Post by jvierra » Fri Oct 23, 2015 1:28 am

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']

Locked