title: How do you call msdeploy from powershell when the parameters have spaces?
body: <p>I'm running into a problem with spaces in my parameters that I try to send into msdeploy from a powershell script.</p>
  
  <p>There are a number of other related articles but none of them solve the problem.
  <br><a href="http://forums.iis.net/p/1163820/1929826.aspx#1929826" rel="nofollow noreferrer">Problems Using Power Shell And MSDeploy.</a>
  <br>Similar SO issue that doesn't work: <a href="https://stackoverflow.com/questions/1673967/how-to-run-exe-in-powershell-with-parameters-with-spaces-and-quotes">How to run exe in powershell with parameters with spaces and quotes</a>
  <br><b>PowerShell BUG:</b><a href="https://connect.microsoft.com/PowerShell/feedback/details/376207/executing-commands-which-require-quotes-and-variables-is-practically-impossible" rel="nofollow noreferrer"> Executing commands which require quotes and variables is practically impossible</a>
  <br>Another SO issue that doesn't work:<a href="https://stackoverflow.com/questions/1773356/passing-parameters-in-powershell-2-0">Passing parameters in PowerShell 2.0</a>
  </p>
  
  <p>The simplest example that succeeds and then fails when I make it more complicated is just dumping the default web site.</p>
  
  <p>$msdeploy = "C:\Program Files\IIS\Microsoft Web Deploy\msdeploy.exe"
  <br>&$msdeploy -verb:dump -source:appHostConfig=`'default web site`' -verbose
  
  <br>==SUCCESS
  </p>
  
  <p>This one?</p>
  
  <p>$sitename="default web site"
  <br>&$msdeploy -verb:dump -source:appHostConfig=$sitename -verbose
  
  <br>==FAIL with the following error
  <br>msdeploy.exe : Error: Unrecognized argument '"-source:"appHostConfig=default'. All arguments must begin with "-".
  <br>At C:\xxx\test.ps1:122 char:6
  <br>+     & 
  <br>    + CategoryInfo          : NotSpecified: (Error: Unrecogn...begin with "-".:String) [], RemoteException
  <br>    + FullyQualifiedErrorId : NativeCommandError
  
  <br>Error count: 1.
  </p>
  
  <p>The following variations have also failed
  <br>#FAIL
  <br>$sitename=`'default web site`'
  <br>$sitename=`'"default web site"`'
  <br>$sitename="`'default web site`'"
  <br>$sitename="default web site"    
  <br>$sitename="'default web site'"
  </p>
  
  <p>&$msdeploy -verb:dump "-source:appHostConfig=$sitename" -verbose
  <br>&$msdeploy -verb:dump -source:appHostConfig="$sitename" -verbose 
  <br>&$msdeploy -verb:dump -source:appHostConfig='$sitename' -verbose 
  <br>&$msdeploy -verb:dump -source:appHostConfig=`'$sitename`' -verbose 
  <br>&$msdeploy -verb:dump -source:appHostConfig=`"$sitename`" -verbose
  </p>
  
  <p>I'm at a loss. Everyone I work with is at a loss. Seriously this sucks. I loved Powershell. I loved msdeploy. I can't say that I love putting them together. It looks like it may have been easier to focus on the API instead of the cli.</p>
  
  <h3>EDIT:</h3>
  
  <p>The parameters in the string array suggested by Emperor XLII works well. An alternative solution is presented in the following article: <a href="http://trycatchfail.com/blog/post/The-trials-and-tribulations-of-using-MSDeploy-with-PowerShell.aspx" rel="nofollow noreferrer">The trials and tribulations of using MSDeploy with PowerShell</a> </p>
  
  <pre><code>function PushToTarget([string]$server, [string]$remotePath, [string]$localPath) {
      cmd.exe /C $("msdeploy.exe -verb:sync -source:contentPath=`"{0}`" -dest:computerName=`"{1}`",contentPath=`"{2}`" -whatif" -f $localPath, $server, $remotePath )
  }
  </code></pre>