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

Re: Passing parameters to exe

Post by fergusfog »

Thanks for your help.

Maybe I'm confused by the first part ending with:

Results:

$Parameter[0] = –Parameter

and then continuing with:

It can be taken even further by creating a function called Convert-ArgumentsToDictionary...

This tells me the bit that follows is optional, but you seem to be suggesting the 2nd (Dictionary) part is required as well..?

For that reason, I was taking the first bit only and trying to make that work. I'll give it a go with the rest too.
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 ony part that is default is handing the copy of the command line. You have to call the functions to get a dictionary. YO also have to understand what arguments are and how they are used programmatically.

The functions are basically PowerShell versions to the standard "C" command parsers.
User avatar
fergusfog
Posts: 13
Last visit: Tue Oct 27, 2015 4:01 am

Re: Passing parameters to exe

Post by fergusfog »

Okay, I'm almost there and understanding it a little more.

The last bit, however, is not giving me any output.

Your previous post with:
$myargs=Parse-Commandline $CommandLine
#Convert the Arguments. Use – as the Argument Indicator
$named=Convert-ArgumentsToHashtable $myargs ‘-‘

Well, I copied the code from the article so I'm using $dictionary instead of $named, but trying to get the value for the -imei arg I passed to the exe doesn't give me anything. I'm running the exe with "-imei 333333", so I'm trying to get the code to return "33333333" for me:
$1 = $Dictionary['imei']
Write-Host $1
but this is empty.

(Btw it does output $dictionary in a nice formatted table, so the data is in there, but I just have the wrong code to get it out.)

When this is done, I'm investing in a PS book...
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 »

Here is an example.

Build and test with

.\TestArgs.exe -test 456789
Attachments
TestArgs.ps1
(8.84 KiB) Downloaded 232 times
User avatar
fergusfog
Posts: 13
Last visit: Tue Oct 27, 2015 4:01 am

Re: Passing parameters to exe

Post by fergusfog »

Wow. Excellent. Many thanks for all your help.

I've incorporated it into my MSOnline scripts and it works like a charm.

Tbh I can't see what's different from what I was doing, but I'm not complaining. :-)

Cheers again.

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 »

Great. The discussion may be a bit confusing if you are not up to speed in PowerShell. I just grabbed everything and ran it. The original code I posted was a mix of both techniques an was probably misleading and wouldn't have worked if you didn't have all (new and old) functions loaded.
This topic is 8 years and 4 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