Solved

Converting Bat to PowerShell

Posted on 2014-03-31
6
3,332 Views
Last Modified: 2014-04-07
I am converting a Bat to a PS1.

In the bat, it calls job    

 call "C:\Program Files\xyz.exe" %*   passing the complete parameter list entered on the command line to xyz.exe.  In Dos, you just use the %* to send the whole line unparsed.

I want to mimic the %*.

How do I do this in Powershell?
0
Comment
Question by:HCSHAW
6 Comments
 
LVL 18

Assisted Solution

by:Raheman M. Abdul
Raheman M. Abdul earned 200 total points
ID: 39967437
start-process "C:\Program Files\xyz.exe" $args
0
 

Author Comment

by:HCSHAW
ID: 39967656
In digging deeper into the code the following emerges.

The xyz.exe is really xyz.cmd which is a dos batch file renamed with a cmd extension.

I know that to call a dos batch in PS, this should work

 
& Cmd.exe /C "C:\TEST\XYZ.cmd"  

So should this work

& Cmd.exe /C "C:\TEST\XYZ.cmd" $Arg

To test this , I created a PS1 named Call_it.ps1 and then compiled it to an EXE (call_it.exe)

The source looks like this:
write-host "Args:" $arg
& cmd.exe /C "C:\TEST\xyz.cmd"  $arg

In running it from PowerGUI Script Editor I use this command
 
.\Call_it.exe  -t 1232axd  -m h5h4hkj3k2

Shouldn't I be able to see the the $arg displayed?

I know mixing dos and PS1 has its issues, but it will just have to be this way for the time being.  The call_it.exe has be that name.  External software that I have no control over calls using that name.  Second, the xyx.cmd has to stay a dos file since it is a very complicated piece of code that works and I do not  have the time to convert it to PS1.  My goal is to create the PS1-exe to call the cmd and get it working.  

I appreciate the help.
0
 

Author Comment

by:HCSHAW
ID: 39970338
Just to clarify

Program 1 (call_it.exe) is called with parameters.
Program 1 receives the parameters and then calls program2 (xyz.cmd) passing the whole command line along to program 2.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 67

Expert Comment

by:sirbounty
ID: 39977763
$args is what you need to use, not $arg.

If I were going to try to recreate what you've described, I would create a batch file like this:
powershell.exe .\test.ps1 abc def ghi

Open in new window

and inside my powershell script, I would have:
write-host "Number of arguments passed is $($args.count)"
foreach ($arg in $args) {
  write-host "Arg value is $arg"
}

Open in new window


But it looks like you're trying to use named parameters, so I would alter that just a bit to be:

write-host "Number of arguments passed is $($args.count)"
for ($x=0;$x -le $args.count -1;$x++) {
    if ($args[$x].contains("-")) {
      write-host "Value of $($args[$x]) is $($args[$x+1])"
    }
}

Open in new window

0
 
LVL 68

Accepted Solution

by:
Qlemo earned 300 total points
ID: 39979976
All you need to do is just call the batch with $args. There is nothing special about that:
C:\TEST\xyz.cmd $args

Open in new window

No need to use the ampersand operator unless the path contains spaces.
& "C:\Program Files\Vender\App\app.cmd" $args

Open in new window

As a next step you could name the parameter of the script, so they are assigned correctly, but there is no need to do that:
param ($t, $m)
C:\TEST\xyz.cmd -t $t -m $m $args

Open in new window

In this case $args contains all other parameters not named in the param statement.
0
 

Author Closing Comment

by:HCSHAW
ID: 39982983
Raheman l had it pegged to start with, but I needed a little more in-dept discussion which  Qlemo provided.  I appreciate all who contributed, it's a great way to learn when you can pick an experts brain.    

I have tried the recommendations but ran into a snag when I converted the PS1 to an EXE.  It seems once the PS1 is encapsulated, it is not recognizing the parms.   I have another question posted where I have been trying to unravel that piece of the puzzle.  Perhaps one of you can read over that and see something obvious that I am overlooking.  

http://www.experts-exchange.com/Programming/Languages/Scripting/Powershell/Q_28402743.html

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

Thank you again for your help.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
Set OWA language and time zone in Exchange for individuals, all users or per database.
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

759 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

19 Experts available now in Live!

Get 1:1 Help Now