Solved

Converting Bat to PowerShell

Posted on 2014-03-31
6
3,421 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
A short film showing how OnPage and Connectwise integration works.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

929 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

12 Experts available now in Live!

Get 1:1 Help Now