PowerShell& MSDeploy - 空格参数 [英] PowerShell & MSDeploy - Arguments with Spaces

查看:307
本文介绍了PowerShell& MSDeploy - 空格参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法解决如何使用msdeploy.exe和PowerShell v4传递包含空格的文件夹的参数。

I cannot work out how to pass arguments that contain folders with spaces using msdeploy.exe and PowerShell v4.

示例Powershell脚本

Sample Powershell Script

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

当文件夹名称没有空格时,它工作正常,但是它有一个空间失败:

When the folder name has no spaces, it works fine, however when it has a space it fails:

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:

Manually calling msdeploy.exe using the following command:

msdeploy -verb:sync -source:filePath =d:\space space\a.txt-dest:filePath =d:\space space\b.txt

这个命令提示符工作正常,但PowerShell不起作用。

This works fine from Command Prompt but does not work from PowerShell.

这个博客作为一个援助,但没有任何运气: http://trycatchfail.com/blog/post/The-trials-and-tribulations-of-using-MS部署与PowerShell.aspx

I have used this blog as an aid but without any luck: http://trycatchfail.com/blog/post/The-trials-and-tribulations-of-using-MSDeploy-with-PowerShell.aspx

我已经看了更多的例子。如果执行标准的复制操作powershell可以将路径传递给cmd.exe(copy)。

I have looked into some more examples. If you perform a standard copy operation powershell is able to pass the path to cmd.exe (copy).

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代码片段仍然因为空间而失败。

Using the same approach to update the msdeploy snippet still fails because of the space.

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



一个解决方案



http://stackoverflow.com/a/12813048/1497635

我想补充说,三个逃生字符绝对是疯狂的。

I would like to add that three escape characters is absolutely crazy. There must be a neater solution to the problem.

推荐答案

我使用以下建议:
如何从powershell调用msdeploy当参数具有空格时?

导出更干净的解决方案。

To derive a "cleaner" solution.

    $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& MSDeploy - 空格参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆