Solved

A PS1 compiled   to EXE does not recognize the passed parameters.

Posted on 2014-04-01
12
1,371 Views
Last Modified: 2014-04-09
Powershell Newbie here.

Using PowerGUi Script Editor, I compile a PS1 to EXE.   When I run the EXE it does not see the passed parameters.  I am running them from the PowerShell Console in the PowerGUI Script Editor.

Here is the program code.

param([string]$foo , [string]$bar )
Write-Host "Arg0: $foo"
Write-Host "Arg1: $bar"
Write-Host "Arg2: $who"

Here are the test results
--Calling EXE--
PS C:\test> .\blat2.exe it bugs me
Arg0:
Arg1:
Arg2:

--Calling PS1--
PS C:\test> .\blat2.ps1 it bugs me
Arg0: it
Arg1: bugs
Arg2: me

Being a newbie, I'm pretty sure I am making a rookie mistake.  
How do I get the exe to recognize the passed parameters?
0
Comment
Question by:HCSHAW
  • 6
  • 3
  • 2
12 Comments
 
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 39971584
In your program code, did you forget to type the last parameter in the param list?  $who
0
 
LVL 19

Expert Comment

by:Raheman M. Abdul
ID: 39971819
Try this :
 .\blat2.exe -arguments -foo "it" -bar "bugs"  -who "me"
0
 

Author Comment

by:HCSHAW
ID: 39973117
Yes I did forget to show the Parameter $Who.  It was a typo.

Corrected to read param([string]$foo , [string]$bar ,  [string]$who )


The program  is being called by a software that I have no control over, so I cannot change the way it calls blat2.exe.

A typical command line would be  

blat2.exe -t something  anotherthing  

So the expected parameters would be  

-t
something
anotherthing
 


So blat2 has to accept the command line as it is sent, I cannot change the program that calls  blat2.exe

My issue is that I can get the $args to show up if it is is PS1 run, but after compiling they quit showing up.  It's like the compile is preventing the program from accepting the parameters.


Why does the EXE version not  recognize the incoming parameters?
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

Author Comment

by:HCSHAW
ID: 39983276
I have tried this with the -arguments option and it works.   However, this does not help me solve the problem I have.

.\blat2.exe -arguments -t somthing something else

The software that is calling the blat2.exe cannot be modified to add the -arguments into the call.

Is there any other way to get the exe to accept the args passed without having to add the in the -arguments to the call?
0
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 39983644
Bad news. I've performed some tests, and it seems you cannot get around that "limitation". Anything provided and not being a subset of the PowerShell.exe parameters (like -NoExit, -NoBanner) will just be ignored. Even $myInvocation does not have anything useful here.

Good news. You can get the commandline, and remove the script/exe name:
$ownarg = [Environment]::GetCommandLineArgs()
write-host $ownarg[1..$ownarg.Count]

Open in new window

0
 

Author Comment

by:HCSHAW
ID: 39984199
That seems very promising.  I'll give that a try here shortly.  I have it working in test, but am testing with the real code.
0
 

Author Comment

by:HCSHAW
ID: 39985398
I've requested that this question be closed as follows:

Accepted answer: 0 points for HCSHAW's comment #a39984199

for the following reason:

That worked!  Thanks for that.  I have looked everywhere for some way to do this, only to be stumped again and again.

Here is my final code for those that are following up on this later.

SOURCE CODE ----------------------------------------------------
$ownarg = [Environment]::GetCommandLineArgs()
& cmd.exe /C "C:\TEST\Blat2.exe"  $ownarg[1..$ownarg.Count]
 

I then complied it using POWERGUI to an EXE
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 39985399
Please accept my comment instead of yours.
And, as said, the call to the batch file is unnecessarily complicated.
0
 

Author Comment

by:HCSHAW
ID: 39986026
Sorry about that.   Thanks for getting it corrected.
I understand the call to the batch is complicated, but I am kinda stuck right now with what I have.   This works and buys me some time.   Thank you again for you help.
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 39987291
Again, to be clear:
& cmd.exe /C "C:\TEST\Blat2.exe"  $ownarg[1..$ownarg.Count]

Open in new window

can be written as
C:\TEST\Blat2.exe $ownarg[1..$ownarg.Count]

Open in new window

0
 

Author Comment

by:HCSHAW
ID: 39988765
Thanks.
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question