Powershell和作为参数的路径是双引号分隔的

joe*_*alt 7 powershell powershell-2.0

我有一个简单的PS脚本,需要接受恰好是目录路径的参数.我把这条路径交给我并调用ps脚本如下:

powershell.exe -ExecutionPolicy Bypass -F "C:\temp\ctest\logging test\postinstall.ps1" "C:\temp\ctest\logging test\"
Run Code Online (Sandbox Code Playgroud)

我无法控制将"\"添加到作为此脚本的参数的路径,并且必须使用双引号来说明路径中的空间.所以,我最终得到的是我的ps脚本中的一个变量,即字符串:

C:\temp\ctest\logging test"     <<-- error in path!  with the double-quote char. :(
Run Code Online (Sandbox Code Playgroud)

我希望,我的问题很简单,但我无法找到解决它的人.在这种情况下,有没有办法告诉powershell不要逃避最后的双引号?

感谢您的时间,并教育我.

And*_*ndi 3

该问题似乎仅在从 CMD 调用时出现。在你的脚本中你可以这样做:

$args[0].TrimEnd('"')
Run Code Online (Sandbox Code Playgroud)

如果存在尾随双引号,它将删除它。

或者你可以加倍反斜杠:

C:\>powershell.exe -f C:\echo.ps1 "C:\temp\ctest\logging test\\"
Run Code Online (Sandbox Code Playgroud)

echo.ps1 的内容

Write-Host ('"{0}"' -f $args[0])
Run Code Online (Sandbox Code Playgroud)