【发布时间】:2014-09-28 23:08:33
【问题描述】:
我不知道如何使用 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
更新
我研究了更多示例。如果您执行标准的复制操作,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 sn -p 还是因为空间问题而失败。
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
我想补充一点,三个转义字符绝对是疯狂的。这个问题必须有一个更简洁的解决方案。
【问题讨论】:
-
相关:stackoverflow.com/questions/3499699/… 这些建议没有帮助。
-
您确定需要 msdeploy.exe "-verb:sync",$arg1,$arg2 中的逗号吗?
-
从技术上讲,它是一个 [System.Object[]]。可以使用 [System.Object[]]$args = "argument 1", "argument 2", "argument 3" 构建它只是选择了上面的方式来简化。它被迅速拼凑起来,以便有人快速重新生产它。这个问题可能与 MSDeploy 本身无关,而是 PowerShell 如何将参数传递给其他进程,如 msdeploy.exe 甚至 cmd.exe
-
需要将 MSDEPLOY.EXE 的路径添加到环境变量中才能正常工作:Path = C:\Program Files\IIS\Microsoft Web Deploy V3
-
使用Web Deploy PowerShell Cmdlets 提供了另一种更优雅的解决方案。它还解决了使用 Invoke-Expression、Start-Process 和 Call/& 运算符时可能会遇到的输出重定向问题。
标签: powershell deployment powershell-3.0 msdeploy webdeploy