如何处理 powershell 命令中的点?

How to handle dots in powershell commands?

下面的命令在命令行中有效

mvn clean install -Denunciate.skip 

但在 powershell 中中断并出现错误

[错误] 未知的生命周期阶段“.skip”。您必须以

格式指定有效的生命周期阶段或目标

经过反复试验,这对我有用(我通过将 Denunciate.skip 括在引号中将其视为字符串)

 mvn clean install -"Denunciate.skip"

使用引号会有帮助,尤其是对于实际的 PowerShell 代码。但是,您只是在尝试使用常规命令。您可以使用 stop-parsing parameter

来防止 PowerShell 解析器误解您的代码

The stop-parsing symbol (--%), introduced in Windows PowerShell 3.0, directs Windows PowerShell to refrain from interpreting input as Windows PowerShell commands or expressions.

When calling an executable program in Windows PowerShell, place the stop-parsing symbol before the program arguments. This technique is much easier than using escape characters to prevent misinterpretation.

所以对于您的命令,您也可以这样做。

mvn --% clean install -"Denunciate.skip"

如果您确实在其中混合了变量,则只需根据需要移动停止解析器。

我觉得我必须确认并添加到 developer747 的答案中。双引号需要完全包裹每个参数,以便 PowerShell 停止处理参数内容。 (不要像 -foo.bar="baz" 那样把双引号放在参数的深处)。

  • 在 PowerShell 提示符下:

    function detect() { write-host "in detect"; write-host "arg 0: $($args[0])"; write-host "arg 1: $($args[1])"; } exit detect "-foo.bar=baz"

    in detect
    arg 0: -foo.bar=baz
    arg 1:
    
  • 在 PowerShell 提示符下:

    function detect() { write-host "in detect"; write-host "arg 0: $($args[0])"; write-host "arg 1: $($args[1])"; } exit detect -foo.bar=baz

    in detect
    arg 0: -foo
    arg 1: .bar=baz
    
  • 在 PowerShell 提示符下:

    function detect() { write-host "in detect"; write-host "arg 0: $($args[0])"; write-host "arg 1: $($args[1])"; } exit detect -foo.bar="baz"

    in detect
    arg 0: -foo
    arg 1: .bar=baz
    

为了将双引号从 CMD 传递到 PowerShell,我必须在它们前面加上反斜杠(而不是插入符号!),

  • 在 CMD 提示符下:

    powershell -Command function detect() { write-host "in detect"; write-host "arg 0: $($args[0])"; write-host "arg 1: $($args[1])"; } exit detect \"-bar.foo=baz\"

    in detect
    arg 0: -bar.foo=baz
    arg 1: