Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-04-01
12
Medium Priority
?
1,475 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

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

Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist today!

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
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ā€¦
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastrā€¦

705 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