Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-04-01
12
Medium Priority
?
1,499 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
WatchGuard Case Study: Museum of Flight

“With limited money and limited staffing, we didn’t have a lot of choices in terms of what we could do to bring efficiency. WatchGuard played a central part in changing that.” To provide strong, secure Wi-Fi access within the museum, Hunter chose to deploy WatchGuard’s AP120 APs.

 

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 71

Accepted Solution

by:
Qlemo earned 2000 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 71

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 71

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

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

Question has a verified solution.

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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Transferring FSMO roles is done when an admin wants to split roles between certain Domain Controllers or the Domain Controller holding the Roles has been forcefully demoted using dcpromo / forceremoval
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…

885 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