3

I want the $mydate variable or the command Get-Date -Format yyyy-MM-dd to expand in the following command line (note the curly braces required by the svn.exe client:

$mydate = Get-Date -Format yyyy-MM-dd

svn log https://svn.apache.org/repos/asf/ -r {Get-Date -Format yyyy-MM-dd}

svn log https://svn.apache.org/repos/asf/ -r {$mydate}

In both cases I am getting the following error:

svn: E205000: Syntax error in revision argument '-encodedCommand'

Why does the variable becomes -encodedCommand? Should I escape curly braces? How? Tick '`' does not work:

Error formatting a string: Input string was not in a correct format..
At line:1 char:1
+ svn log https://svn.apache.org/repos/asf/ -r `{$mydate`}

What am I doing wrong?

bahrep
  • 29,961
  • 12
  • 103
  • 150

2 Answers2

2

I don't have enough points to comment, but try:

svn log https://svn.apache.org/repos/asf/ -r "{$($mydate)}"

The outer " quotes convert it all into a string, so ignores the curly braces. The $() allows the variable to be correctly interpreted (not as a string).

bahrep
  • 29,961
  • 12
  • 103
  • 150
yaquaholic
  • 152
  • 1
  • 11
  • 2
    Nice, though the `$(...)` isn't strictly necessary here (`"{$mydate}"` will do), given that the only thing to interpolate is a simple variable reference, not an expression; for the full rules, see https://stackoverflow.com/a/40445998/45375 – mklement0 Oct 09 '18 at 13:54
2

To complement yaquaholic's helpful answer with background information:

Unquoted use of {...} has special meaning in PowerShell: it creates a script block (type [scriptblock]), which is a reusable piece of PowerShell code that can be passed as an argument or stored in a variable for later execution on demand.

Therefore, to pass arguments with embedded { or } characters, quote them (with '...' (literal string, e.g., '{foo}') or "..." (expandable string, e.g. "{$foo}"), as needed).


The behavior that PowerShell exhibits as of Windows PowerShell v5.1 / PowerShell Core 7.0 with unquoted {...} is a known problem:

A script block has no meaning outside of PowerShell, such as when passing arguments to an external program like svn.

By contrast, calling PowerShell's own CLI - powershell.exe (Windows PowerShell), pwsh (PowerShell Core) - with a script block is supported, via behind-the-scenes Base64-encoding of the script block's content, with the encoded string passed via -encodedCommand, and CLIXML serialization applied to arguments and pipeline input - see this comment on GitHub

This mechanism is currently - pointlessly - applied to other external programs too, which is why you saw an -encodedCommand argument appear.

mklement0
  • 382,024
  • 64
  • 607
  • 775