使用msdeploy在powershell中使用变量中的空格

时间:2015-01-09 11:57:14

标签: variables powershell escaping whitespace msdeploy

我在使用powershell和msdeploy的变量中存在空格问题。 这就是我需要的:

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

cd 'C:\Windows\DtlDownloads\WebServices\WebServices\IdService\_PublishedWebsites\IdService_Package'

[System.Collections.ArrayList]$msdeployArgs = [string[]]@(
  "-verb:sync",
  "-source:package='IdService.zip'",
  "-verbose",
  "-dest:auto"
  "-setParam:Environment=$IdConnectionString"
  )

& $msdeploy $msdeployArgs

这是错误消息:

msdeploy.exe : Error: Unrecognized argument '"-setParam:Environment=Werk niet"'. All arguments must begin with "-".
At C:\Windows\DtlDownloads\WebServices\WebServices\IdService\_PublishedWebsites\IdService\Deployment\IdServiceWSDeploy.ps1:23 
char:1
+ & $msdeploy $msdeployArgs
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Error: Unrecogn...begin with "-".:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

Error count: 1.

这也有效:

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

cd 'C:\Windows\DtlDownloads\WebServices\WebServices\IdService\_PublishedWebsites\IdService_Package'

[System.Collections.ArrayList]$msdeployArgs = [string[]]@(
  "-verb:sync",
  "-source:package='IdService.zip'",
  "-verbose",
  "-dest:auto"
  )


& $msdeploy $msdeployArgs -setParam:Environment=`'Werk niet`'

但我真的需要变量进行自动部署。正常情况下,$IdConnectionString变量在与发布管理一起使用的另一个配置脚本中定义。

2 个答案:

答案 0 :(得分:0)

以这种方式尝试:

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

$msdeployArgs = @{
  verb = 'sync'
  source = "package='IdService.zip'"
  verbose = $true
  dest = 'auto'
  setParam = "Environment='$IDConnectionString'"
  }

  Invoke-Expression "& '$msdeploy' $(&{$args}@msdeployArgs)"

编辑:大多数问题似乎都围绕着引用。在查看msdeploy文档之后,我认为最简单的解决方案是在可扩展的字符串中简单地构建命令:

$ IdConnectionString =“Hello World” $ msdeploy =“C:\ Program Files \ IIS \ Microsoft Web Deploy V3 \ msdeploy.exe”

$cmd = @"
& '$msdeploy' -verb:Sync -Source:package='IDService.zip' -verbose -dest:auto -setParam:Environment='$IdConnectionString'
"@

invoke-expression $cmd 

我无法真正复制您的环境进行测试,但基本上您只需要找到命令提示符下的命令字符串,然后在here字符串中复制它,然后调用它。

答案 1 :(得分:0)

我认为你可能需要像这样使用Start-Process:

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

$msdeployArgs = @(
  "-verb:sync",
  "-source:package='IdService.zip'",
  "-verbose",
  "-dest:auto"
  "-setParam:Environment=`"$IdConnectionString`""
)

Start-Process $msdeploy -NoNewWindow -ArgumentList $msdeployArgs