Passing parameters to exe

Ask your 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.
This topic is 8 years and 5 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.
Locked
User avatar
fergusfog
Posts: 13
Last visit: Tue Oct 27, 2015 4:01 am

Passing parameters to exe

Post 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.
User avatar
SAPIEN Support Forums
Posts: 945
Last visit: Thu Oct 22, 2015 1:10 pm

Passing parameters to exe

Post 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 ***
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Passing parameters to exe

Post by jvierra »

$commandline | %{Write-Host $_ -fore green}
User avatar
fergusfog
Posts: 13
Last visit: Tue Oct 27, 2015 4:01 am

Re: Passing parameters to exe

Post by fergusfog »

That outputs exactly the same as

Write-Host $commandline

...except in green.
User avatar
Alexander Riedel
Posts: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: Passing parameters to exe

Post 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.
Alexander Riedel
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Passing parameters to exe

Post by jvierra »

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: 8478
Last visit: Tue Mar 26, 2024 8:52 am
Answers: 19
Been upvoted: 37 times

Re: Passing parameters to exe

Post by Alexander Riedel »

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
Last visit: Tue Oct 27, 2015 4:01 am

Re: Passing parameters to exe

Post 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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Passing parameters to exe

Post 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.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Passing parameters to exe

Post 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']
This topic is 8 years and 5 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.
Locked