2

I'm using invoke-expression in PowerShell to create an archive, but it's not working due to spaces in the exe path. These are my variables:

Set-Variable -name DIRRELEASE       -value (Join-Path $env:UserProfile "\Documents\Coding\_Projects\ChickenPing\trunk\Dist\current\")
$srcPath = (Join-Path $DIRRELEASE ("myapp_1.90-src.zip"))
Set-Variable -name WinRarFilter     -value "-x*\.svn -x*\.svn\* -x*\nutrition.db3"
Set-Variable -name WinRarOpts       -value "-r -s -m5 -inul"
$WinRar = `"C:\Program Files\Winrar\winrar.exe`"

#Then using this to invoke:
Invoke-Expression ($WinRAR + " a " + $srcPath + " " + $WinRARFilter + " * " + $WinRAROpts)

When I run the script I get this error:

The term 'a' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:3 + a <<<< C:\Users\Echilon\Documents\Coding_Projects\MyApp\trunk\Dist\c urrent\myapp_1.95-src.zip -x*.svn -x*.svn* -x*\nutrition.db3 * -r -s - m5 -inul + CategoryInfo : ObjectNotFound: (a:String) [], CommandNotFoundEx ception + FullyQualifiedErrorId : CommandNotFoundException

I just can't find the right combination of quotes and plusses.

Echilon
  • 10,064
  • 33
  • 131
  • 217

1 Answers1

4

You could make this a lot easier to work with by using the call operator &.

& $WinRAR a $srcPath "-x*\.svn" "-x*\.svn\*" "-x*\nutrition.db3" *  -r -s -m5 -inul
Andy Arismendi
  • 50,577
  • 16
  • 107
  • 124
  • 1
    Command invocation operator is definitely the way to go. If you really want to stick with Invoke-Expression, you could try the format operator (-f) to get your quoting right [untested code follows]: '"{0}" a "{1}" {2} * {3}' -f $WinRar,$srcPath,$WinRARFilter,$WinRAROpts – vinny Feb 10 '12 at 20:19
  • 2
    I never really get why people always try `iex` when calling other programs. Or some even more convoluted ways ... heck, it's a *shell* (not only by name) – its primary purpose is to call commands and programs. – Joey Feb 10 '12 at 20:27
  • I've only found `iex` useful if I have something I want to execute in a text file. – Andy Arismendi Feb 10 '12 at 21:01
  • I usually find an expandable here-string easier to work with than using -f for getting the quoting right. Put whatever kind of quotes you want, whereever you want them, and you don't have to worry about nesting or escaping. – mjolinor Feb 10 '12 at 21:21
  • Unless you need to include a variable with a property. Then you either need to use a subexpression + double quoted here-string or -f on the here-string. – Andy Arismendi Feb 10 '12 at 22:18
  • Yes. But even with the sub-expression, it's more intuitive to read that going back and forth between the format string and the argument list, IMHO. – mjolinor Feb 10 '12 at 23:06
  • Pros and cons. Pro: re-factoring is easier (somewhat of a pain to insert a new string formatter because you have to go back and change all the targets {#}). Con: lose syntax highlighting and intellisense (its all a string so it's more difficult to identify a syntax error). – Andy Arismendi Feb 11 '12 at 00:10
  • I can see losing syntax highlighting as being an isseue. When you're creating command strings for an external command, it's unlikely itellisense would recognize the command and it's parameters and required syntax anyway, so you didn't lose much there. – mjolinor Feb 11 '12 at 15:46
  • What an absolute nightmare, after much experimenting, I got it to work with: `& $WinRAR a $zipPath "-x*\.svn" "-x*\.svn\*" "-x*\nutrition.db3" ($DistName + "-zip") -r -s -m5 -inul` – Echilon Feb 11 '12 at 18:08