2

I'm using Powershell(V4) and I'm following the code give here,however, it gives me an error when I run the code.

My Code:

[string]$zipPath="C:\Users\someUser\7z.exe"
[string]$parameters= 'a', '-tzip','C:\Users\someUser\Desktop\Archive.zip','C:\Users\someUser\Desktop\Test'

Powershell View:

PS C:\Users\someUser> $zipPath="C:\Users\someUser\7z.exe" $parameters= 'a', '-tzip','C:\Users\someUser\Desktop\Archive.zip','C:\Users\someUser\Desktop\Test' & $zipPath $parameters

& $zipPath $parameters

Output:

7-Zip (A) 9.20  Copyright (c) 1999-2010 Igor Pavlov  2010-11-18


 Error:
 Incorrect command line
Community
  • 1
  • 1

2 Answers2

1

Try using Start-Process with $parameters as the -ArgumentList:

Start-Process $zipPath -ArgumentList $parameters -wait
arco444
  • 22,002
  • 12
  • 63
  • 67
  • Yes, that worked as well. Thanks. Just out of curiosity, why didn't work the way the example showed? –  Feb 02 '15 at 16:57
  • It should've worked if you'd just had it as a normal string, but I guess `&` doesn't know how to parse an array to get program arguments, whereas `Start-Process` does. – arco444 Feb 02 '15 at 17:02
0

That passes all of your arguments as a single string e.g.:

2> $ec = 'echoargs'
3> & $ec $parameters
Arg 0 is <a -tzip C:\Users\someUser\Desktop\Archive.zip C:\Users\someUser\Desktop\Test>

Command line:
"C:\Users\hillr\Documents\WindowsPowerShell\Modules\Pscx\Apps\EchoArgs.exe"  "a -tzip C:\Users\someUser\Desktop\Archive.
zip C:\Users\someUser\Desktop\Test"

Just pass your arguments normally:

4> & $ec a -tzip C:\Users\someUser\Desktop\Archive.zip C:\Users\someUser\Desktop\Test
Arg 0 is <a>
Arg 1 is <-tzip>
Arg 2 is <C:\Users\someUser\Desktop\Archive.zip>
Arg 3 is <C:\Users\someUser\Desktop\Test>

Command line:
"C:\Users\hillr\Documents\WindowsPowerShell\Modules\Pscx\Apps\EchoArgs.exe"  a -tzip C:\Users\someUser\Desktop\Archive.z
ip C:\Users\someUser\Desktop\Test

BTW echoargs is a tool from the PowerShell Community Extensions.

Keith Hill
  • 194,368
  • 42
  • 353
  • 369