PowerShell& MSDeploy - 带空格的参数

时间:2014-08-06 05:59:39

标签: powershell deployment powershell-v3.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

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

4 个答案:

答案 0 :(得分:3)

我使用了以下建议: 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`""

答案 1 :(得分:3)

调用命令时,PowerShell执行一些与MSDeploy无法正常工作的自动引用。有几种方法可以避免自动引用。一种方法是使用Start-Process cmdlet,您可以在其中指定所需的确切命令行,但是将新进程的输出显示为您正在运行的PowerShell脚本的输出可能会变得有点繁琐。

另一种选择是使用--%说明符关闭PowerShell解析。但是,这样做不允许您在命令行中使用变量,因为 - 好吧,解析已经关闭。但是,您可以使用Invoke-Expression cmdlet首先构建包含--%的命令行以及您想要的任何变量,然后让PowerShell对其进行评估,从而解决这个问题:

$fl1 = "D:\space space\a.txt";
$fl2 = "D:\space space\b.txt";
$arguments = "-verb:sync -source:filePath=""$fl1"" -dest:filePath=""$fl2"""
$commandLine = 'msdeploy.exe --% ' + $arguments
Invoke-Expression $commandLine

答案 2 :(得分:1)

我发现这有效:

$arguments=@(
"-verb:sync"
,"-source:metakey=lm/$IISSite,computername=$computer,includeAcls=true"
,"-dest:metakey=lm/w3svc/$DestSite"
,"-enableLink:appPool"
,"-allowUntrusted"
,"-skip:attributes.name=ServerBindings"
,"-skip:attributes.name=SecureBindings"
#,"-whatif"
)


Write-Output "Running MSDeploy with the following arguments"
$arguments
$logfile="Sync_$(get-date -format yyyyMMdd-HHmm).log"
Start-Process -FilePath "$msdeploy\msdeploy.exe" -ArgumentList $arguments -WorkingDirectory $msdeploy -RedirectStandardError "Error.txt" -RedirectStandardOutput $logfile -Wait -NoNewWindow

答案 3 :(得分:1)

找到一个简单的解决方案。参考:http://answered.site/all-arguments-must-begin-with--at-cwindowsdtldownloadswebserviceswebservicesidservicepublishedwebsitesidservicedeploymentidservicewsdeployps123/4231580/

$msdeploy = "C:\Program Files\IIS\Microsoft Web Deploy V3\msdeploy.exe"
$msdeployArgs = @(
"-verb:sync",
"-source:iisApp='Default Web Site/HelloWorld'",
"-verbose",
"-dest:archiveDir='c:\temp1'"
)
Start-Process $msdeploy -NoNewWindow -ArgumentList $msdeployArgs