我无法弄清楚如何使用 msdeploy.exe 和 PowerShell v4 传递包含带空格的文件夹的参数。
示例 Powershell 脚本
write-warning "WITHOUT SPACE"
$fl1 = "d:\nospace\a.txt"
$fl2 = "d:\nospace\b.txt"
$arg1 = "-source:filePath=`"$fl1`""
$arg2 = "-dest:filePath=`"$fl2`""
msdeploy.exe "-verb:sync",$arg1,$arg2
write-warning "WITH SPACE"
$fl1 = "d:\space space\a.txt"
$fl2 = "d:\space space\b.txt"
$arg1 = "-source:filePath=`"$fl1`""
$arg2 = "-dest:filePath=`"$fl2`""
msdeploy.exe "-verb:sync",$arg1,$arg2
当文件夹名称没有空格时,它可以正常工作,但是当它有空格时,它会失败:
msdeploy.exe : Error: Unrecognized argument '"-source:filePath="d:\space'. All arguments must begin with "-".
At E:\PAWS\Payroll System\PES-Branch-FW\Publish\DeployPackage.ps1:253 char:9
+ msdeploy.exe "-verb:sync",$arg1,$arg2
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (Error: Unrecogn...begin with "-".:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Error count: 1.
使用以下命令手动调用 msdeploy.exe:
msdeploy -verb:sync -source:filePath="d:\space space\a.txt" -dest:filePath="d:\space space\b.txt"
这在命令提示符下工作正常,但在 PowerShell 下不起作用。
我使用这个博客作为帮助,但没有任何运气:http://trycatchfail.com/blog/post/The-Trials-and-tribulations-of-using-MSDeploy-with-PowerShell.aspx
Update
我还研究了一些更多的例子。如果执行标准复制操作,powershell 能够将路径传递给 cmd.exe(复制)。
write-warning "WITHOUT SPACE"
$fl1 = "d:\nospace\a.txt"
$fl2 = "d:\nospace\b.txt"
$args = ('"{0}" "{1}"' -f $fl1, $fl2)
write-host $args
cmd /c copy $args
write-warning "WITH SPACE"
$fl1 = "d:\space space\a.txt"
$fl2 = "d:\space space\b.txt"
$args = ('"{0}" "{1}"' -f $fl1, $fl2)
write-host $args
cmd /c copy $args
由于空间问题,使用相同的方法更新 msdeploy 代码片段仍然失败。
write-warning "WITHOUT SPACE"
$fl1 = "d:\nospace\a.txt"
$fl2 = "d:\nospace\b.txt"
$arg1 = '-source:filePath="{0}"' -f $fl1
$arg2 = '-dest:filePath="{0}"' -f $fl2
$args = '-verb:sync',$arg1, $arg2
msdeploy.exe $args
write-warning "WITH SPACE"
$fl1 = "d:\space space\a.txt"
$fl2 = "d:\space space\b.txt"
$arg1 = '-source:filePath="{0}"' -f $fl1
$arg2 = '-dest:filePath="{0}"' -f $fl2
$args = '-verb:sync',$arg1, $arg2
msdeploy.exe $args
一种解决方案
https://stackoverflow.com/a/12813048/1497635
我想补充一点,三个转义字符绝对是疯狂的。必须有一个更简洁的解决方案来解决这个问题。