• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4682
  • Last Modified:

Converting Bat to PowerShell

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
HCSHAW
Asked:
HCSHAW
2 Solutions
 
Raheman M. AbdulSenior Infrastructure Support Analyst & Systems DeveloperCommented:
start-process "C:\Program Files\xyz.exe" $args
0
 
HCSHAWAuthor Commented:
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
 
HCSHAWAuthor Commented:
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
Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

 
sirbountyCommented:
$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
 
QlemoBatchelor, Developer and EE Topic AdvisorCommented:
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
 
HCSHAWAuthor Commented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now