PowerShell&;MSDeploy-带空格的参数

PowerShell&;MSDeploy-带空格的参数,powershell,deployment,powershell-3.0,msdeploy,webdeploy,Powershell,Deployment,Powershell 3.0,Msdeploy,Webdeploy,我无法确定如何使用msdeploy.exe和PowerShell v4传递包含带有空格的文件夹的参数 示例Powershell脚本 写入警告“无空格” $fl1=“d:\nospace\a.txt” $fl2=“d:\nospace\b.txt” $arg1=“-source:filePath=`$fl1`” $arg2=“-dest:filePath=`$fl2`” msdeploy.exe”-动词:sync“,$arg1,$arg2 写警告“带空格” $fl1=“d:\space\a.txt

我无法确定如何使用msdeploy.exe和PowerShell v4传递包含带有空格的文件夹的参数

示例Powershell脚本

写入警告“无空格”
$fl1=“d:\nospace\a.txt”
$fl2=“d:\nospace\b.txt”
$arg1=“-source:filePath=`$fl1`”
$arg2=“-dest:filePath=`$fl2`”
msdeploy.exe”-动词:sync“,$arg1,$arg2
写警告“带空格”
$fl1=“d:\space\a.txt”
$fl2=“d:\space\b.txt”
$arg1=“-source:filePath=`$fl1`”
$arg2=“-dest:filePath=`$fl2`”
msdeploy.exe”-动词:sync“,$arg1,$arg2
当文件夹名称没有空格时,它可以正常工作,但是当它有空格时,它会失败:

msdeploy.exe:错误:无法识别的参数''-source:filePath=“d:\space”。所有参数必须以“-”开头。
在E:\PAWS\Payroll System\PES Branch FW\Publish\DeployPackage.ps1:253 char:9
+msdeploy.exe”-动词:sync“,$arg1,$arg2
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+CategoryInfo:NotSpecified:(错误:未确认…以“-”开头:字符串)[],RemoteException
+FullyQualifiedErrorId:NativeCommandError
错误计数:1。
使用以下命令手动调用msdeploy.exe:

msdeploy-动词:sync-source:filePath=“d:\space space\a.txt”-dest:filePath=“d:\space space\b.txt”

这在命令提示符下可以正常工作,但在PowerShell中不起作用

我用这个博客作为帮助,但没有任何运气:

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

由于空间有限,使用相同的方法更新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
一个解决方案


我想补充一点,三个逃避角色绝对是疯狂的。这个问题必须有一个更简洁的解决方案。

我使用了以下建议:

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

    $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`""

我使用了以下建议:

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

    $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不兼容的自动引用。有几种方法可以避免自动报价。一种是使用cmdlet,您可以在其中指定所需的确切命令行,但将新进程的输出显示为正在运行的PowerShell脚本的输出可能会有点繁琐

另一个选项是使用
-->说明符%
关闭PowerShell解析。但是,这样做将不允许在命令行中使用变量,因为解析已经关闭。但是,您可以通过使用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

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

另一个选项是使用
-->说明符%
关闭PowerShell解析。但是,这样做将不允许在命令行中使用变量,因为解析已经关闭。但是,您可以通过使用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

我发现这是有效的:

$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

我发现这是有效的:

$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

找到了一个简单的解决办法。参考:


找到了一个简单的解决办法。参考:


相关:这些建议没有帮助。您确定需要msdeploy.exe“-verb:sync”、$arg1、$arg2中的COMA吗?从技术上讲,它是一个[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作为替代方案,更加优雅,通过使用提供解决方案。它还解决了调用表达式、启动进程和调用/&运算符时可能遇到的输出重定向问题。相关:这些建议没有帮助。您确定需要msdeploy.exe“-verb:sync”、$arg1、$arg2中的COMA吗?从技术上讲,它是一个[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作为替代方案,更加优雅,通过使用提供解决方案。它还解决了输出重定向的问题,这些问题对于调用表达式、启动进程和Call/&操作符来说可能是一个难题。您是否运行了自己的代码?MSDeploy不关心引号,但如果从powershell执行,它将不起作用。所以为什么要发这篇帖子。@civon:你是对的,我最初的回答完全错了。不过,我已经更新了我的答案,希望现在更有用。谢谢。我确认这个脚本是有效的。我喜欢它,因为它比使用那个愚蠢的转义字符更容易阅读。只需将引号移到$fl1
-source:filePath=”“$fl1”“
您是否运行了自己的代码?MSDeploy根本不在乎