代码之家  ›  专栏  ›  技术社区  ›  bahrep Ahamed

为什么PowerShell中的变量会扩展为“-encodedCommand”,而不是其实际值?

  •  3
  • bahrep Ahamed  · 技术社区  · 6 年前

    我想要那个 $mydate Get-Date -Format yyyy-MM-dd 在以下命令行中展开(请注意 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}
    

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

    为什么变量变为 -encodedCommand ? 我应该摆脱大括号吗?怎么用?勾选“`”无效:

    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`}
    

    2 回复  |  直到 6 年前
        1
  •  2
  •   bahrep Ahamed    6 年前

    我没有足够的观点发表评论,但请尝试:

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

    外部的“引号”将其全部转换为字符串,因此忽略大括号。$()允许正确解释变量(不是字符串)。

        2
  •  2
  •   mklement0    5 年前

    yaquaholic's helpful answer

    无报价 {...} script block (类型 [scriptblock]

    因此, 用嵌入的 { } 他们 (与 '...' (文字字符串,例如。, '{foo}' "..." (可扩展字符串,例如。 "{$foo}" ),根据需要)。


    PowerShell在Windows PowerShell v5.1/PowerShell Core 7.0中显示的行为 {...} 是一个 known problem

    脚本块没有意义 例如,将参数传递给外部程序时,例如 svn .

    相比之下,打电话 powershell.exe (Windows PowerShell), pwsh (PowerShell核心)-带有脚本块 -encodedCommand ,以及应用于参数和管道输入的CLIXML序列化-请参阅 this comment on GitHub

    这种机制目前也毫无意义地应用于其他外部程序,这就是为什么您看到 -编码命令

    推荐文章