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

Re: Passing parameters to exe

Post by fergusfog » Fri Oct 23, 2015 2:04 am

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: 13992
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Passing parameters to exe

Post by jvierra » Fri Oct 23, 2015 2:30 am

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
Joined: Wed Jun 08, 2011 1:15 am

Re: Passing parameters to exe

Post by fergusfog » Sun Oct 25, 2015 12:51 pm

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: 13992
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Passing parameters to exe

Post by jvierra » Sun Oct 25, 2015 1:24 pm

Here is an example.

Build and test with

.\TestArgs.exe -test 456789
Attachments
TestArgs.ps1
(8.84 KiB) Downloaded 125 times

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

Re: Passing parameters to exe

Post by fergusfog » Sun Oct 25, 2015 2:10 pm

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: 13992
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Passing parameters to exe

Post by jvierra » Sun Oct 25, 2015 2:42 pm

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.

Locked