Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

powershell sending multiple parameter to a external command

I am trying to run a external exe from a powershell script.

This exe wants 4 parameters.

I have been trying every combo of invoke-item, invoke-command, & 'C:\program files\mycmd.exe myparam', made a shortcut in C:\ to get rid of the spaces in the path.

I can make it work with one parameter, but not with more. I get various errors.

To sum up, how do you send 4 parameters to an exe?

like image 348
falkaholic Avatar asked Jan 26 '10 01:01

falkaholic


People also ask

How do I pass multiple parameters in PowerShell?

To pass multiple parameters you must use the command line syntax that includes the names of the parameters. For example, here is a sample PowerShell script that runs the Get-Service function with two parameters. The parameters are the name of the service(s) and the name of the Computer.

How do you pass multiple parameters to a function?

Note that when you are working with multiple parameters, the function call must have the same number of arguments as there are parameters, and the arguments must be passed in the same order.

How do you pass parameters to a PowerShell script?

You can pass the parameters in the PowerShell function and to catch those parameters, you need to use the arguments. Generally, when you use variables outside the function, you really don't need to pass the argument because the variable is itself a Public and can be accessible inside the function.


1 Answers

It's best if shown in longhand. Once you see what's going on, you can shorten it down by just using commas between each argument.

$arg1 = "filename1"
$arg2 = "-someswitch"
$arg3 = "C:\documents and settings\user\desktop\some other file.txt"
$arg4 = "-yetanotherswitch"

$allArgs = @($arg1, $arg2, $arg3, $arg4)

& "C:\Program Files\someapp\somecmd.exe" $allArgs

... shorthand:

& "C:\Program Files\someapp\somecmd.exe" "filename1", "-someswitch", "C:\documents and settings\user\desktop\some other file.txt", "-yetanotherswitch"
like image 191
Peter Seale Avatar answered Oct 26 '22 13:10

Peter Seale