2

I am running into a problem executing a command using cmd.exe in PowerShell. The problem is the path to the command has spaces in it. Seems to be a general problem with PowerShell. Below is an extract:

$base_dir = resolve-path ..\  # this path has spaces in it 
$msdeploy = $base_dir\tools\msdeploy\msdeploy.exe

cmd.exe /c $("""$msdeploy"" -verb:sync -source:....")

I need to have the path to msdeploy resolve through variables as the script is used in a continuous integration process.

The command wont execute due to the spaces. I have tried to wrap the command in "" (quotes) but still no luck. How do I format the $msdeploy variable in this instance?

Mike Mackintosh
  • 13,917
  • 6
  • 60
  • 87
Samuel Goldenbaum
  • 18,391
  • 17
  • 66
  • 104

2 Answers2

2

Back-ticking "" around the command should make it work:

cmd.exe /c "`"$msdeploy`" -verb:sync -source:..."
Goyuix
  • 23,614
  • 14
  • 84
  • 128
Torbjörn Bergstedt
  • 3,359
  • 2
  • 21
  • 30
  • That _is_ strange. I tried myself setting it up just like you, with the difference that `$msdeploy` was set up with quotes: `$msdeploy = "$base_dir\..."` (to ensure it's a string, just old habit). And I could call `cmd.exe /c "`"$msdeploy`" -help -verb"` without problems. – Torbjörn Bergstedt Aug 08 '12 at 07:05
2

Try calling everything with the $() like this:

$base_dir = resolve-path ..\  # this path has spaces in it 
$msdeploy = $($base_dir)\tools\msdeploy\msdeploy.exe

cmd.exe /c $("$($msdeploy) -verb:sync -source:....")

Edit: I moved the whole command into the $msdeploy variable, and called the $msdeploy by escaping the quotes. I found this link that had a similar problem, so I adjusted your code to match what worked there.

$base_dir = resolve-path ..\
$msdeploy = $("`"" + $base_dir.Path + "\tools\msdeploy\msdeploy.exe`" -verb:sync -source:....")

cmd.exe /c "`"$msdeploy`""
Community
  • 1
  • 1
Nick
  • 4,302
  • 2
  • 24
  • 38