Powershell and a path as an argument that is double

I have a simple PS script that needs to accept a parameter that happens to be a path to a directory. I get this path handed to me and invoke the ps script as follows:

powershell.exe -ExecutionPolicy Bypass -F "C:tempctestlogging testpostinstall.ps1" "C:tempctestlogging test"

I cannot control the addition of the '' to the path that is the param to this script, and it must be double-quoted to account for the space in the path. So, what I wind up with is a variable inside my ps script that is the string:

C:tempctestlogging test"     <<-- error in path!  with the double-quote char. :(

My question is simple, I hope, yet I cannot locate anyone who has solved it, yet. Is there no way to tell powershell not to escape that last double-quote in this scenario?

Thank you for your time, and for educating me.


The issue looks to be only when invoked from CMD. In your script you could do this:

$args[0].TrimEnd('"')

It will remove a trailing double quote if one exists.

Or you could double up the backslash:

C:>powershell.exe -f C:echo.ps1 "C:tempctestlogging test"

Contents of echo.ps1

Write-Host ('"{0}"' -f $args[0])

尝试像这样:

powershell.exe -ExecutionPolicy Bypass -F 'C:tempctestlogging testpostinstall.ps1' 'C:tempctestlogging test'

您是否在您的调用中尝试过单引号?

链接地址: http://www.djcxy.com/p/10486.html

上一篇: 检查更新与添加的唯一性

下一篇: Powershell和作为参数的路径是双重的