powershell - PowerShell&MSDeploy-带空格的参数

标签 powershell deployment powershell-3.0 msdeploy webdeploy

我无法解决如何使用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代码段仍然会失败。
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

我想补充一点,三个转义字符绝对是疯狂的。必须有一个更整洁的解决方案。

最佳答案

我使用以下建议:
How do you call msdeploy from powershell when the parameters have spaces?

得出“更清洁”的解决方案。

    $msdeploy = "C:\Program Files\IIS\Microsoft Web Deploy V3\msdeploy.exe";

    write-warning "WITHOUT SPACE"
    $fl1 = "d:\nospace\a.txt"
    $fl2 = "d:\nospace\b.txt"

    $md = $("`"{0}`" -verb:sync -source:filePath=`"{1}`" -dest:filePath=`"{2}`"" -f $msdeploy, $fl1, $fl2)
    cmd.exe /C "`"$md`""

    write-warning "WITH SPACE"
    $fl1 = "d:\space space\a.txt"
    $fl2 = "d:\space space\b.txt"

    $md = $("`"{0}`" -verb:sync -source:filePath=`"{1}`" -dest:filePath=`"{2}`"" -f $msdeploy, $fl1, $fl2)
    cmd.exe /C "`"$md`""

关于powershell - PowerShell&MSDeploy-带空格的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25153197/

相关文章:

powershell - 使用 for 循环更改 pscustomobject 数组的值

git - Ansible:如何在一个剧本中的其他主机上运行任务?

meteor - 如何将 meteor 应用程序部署到 DigitalOcean

java - 有没有等同于 Java Jar 或 .NET DLL 的 JavaScript?

powershell - 如何使用名称为*的Get-Counter选择性能计数器

powershell - 用于在Active Directory中提取各种数据字段的Powershell脚本

windows - 使用 New-PsDrive 时 Powershell 作业卡住

azure - 从 Azure VM 检索主机名

PowerShell错误: Cannot move item because the item at xxx is in use

powershell - 当Powershell进入新目录时运行命令