Solved

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

Posted on 2014-04-01
12
1,305 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 18

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 18

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
 

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 68

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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 68

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 68

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now