Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escaping splatted variables in Powershell Invoke-Expression

I'm trying to invoke another application (Beyond Compare) from Powershell which requires an @ in the typical command-line:

C:\deploy>bcompare @static.diff

I've found Powershell's Invoke-Expression, but when I try the following it gives me an error:

PS C:\deploy>Invoke-Expression "bcompare @static.diff"
Invoke-Expression : Cannot expand the splatted variable '@static'. Splatted variables
cannot be used as part of a property or array expression. Assign the result of the 
expression to a temporary variable then splat the temporary variable instead.
At line:1 char:18
    + Invoke-Expression <<<<  "bcompare @static.diff"
    + CategoryInfo          : ParserError: (:) [Invoke-Expression], ParseException
    + FullyQualifiedErrorId : NoPropertiesInSplatting,Microsoft.PowerShell.Comands.InvokeExpressionCommand

I can't get the @ to escape properly here. I've tried the `, @@, putting parts of the command in a temporary variable, but none of them did the trick.

like image 471
jasonrclark Avatar asked Apr 06 '11 19:04

jasonrclark


2 Answers

bcompare '@static.diff'

If in doubt, put it into a string :-)

PS Home:\> args '@static.diff'
argv[0] = "C:\Users\Joey\Batches\args.cmd"
argv[1] = @static.diff
like image 131
Joey Avatar answered Oct 11 '22 16:10

Joey


When I ran into the same problem, I used a backtick to make the @-sign interpreted literally. I wanted to use double-quotes for variable handling as well:

Invoke-Expression "& bcompare `@$compareCommands $file1 $file2"

like image 20
Torbjörn Bergstedt Avatar answered Oct 11 '22 17:10

Torbjörn Bergstedt