running MSDeploy in Powershell

rated by 0 users
This post has 1 Reply | 2 Followers

Not Ranked
Posts 1
wannashreddingtoday Posted: 03-25-2011 4:49 PM

Hi, PS Guru?

I have been trying to write a PS deployment script to automate the deployment process bit for my work. I did some research for couple of weeks now and I'm a bit lost here.

first, I found out the forum what I eventually want to do, http://trycatchfail.com/blog/post/The-trials-and-tribulations-of-using-MSDeploy-with-PowerShell.aspx 

But I found out using cmd /c, running msdeploy.exe is not easy actually that the following function doesn't work

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 )
}

So, I wrote and came down to the following script.

$envMsDeploy = $env:msdeploy
$DirMsDeploy = split-Path -Parent $envMsDeploy
# defined the current location to C:\Program Files\IIS\Microsoft Web Deploy V2 
set-location $DirMsDeploy

$arg = @(
    '-verb:sync';
    '-source:contentPath=' + $buildDroppedLocation;
    '-dest:contentPath=' + $destinationPath;
    ' -whatif > c:\temp\msdeploy.log'
)
 
$runMSDeploy = ($DirMsDeploy + "\Msdeploy.exe " + $arg) 

When I outfile $runMSDeploy, it shows "C:\Program Files\IIS\Microsoft Web Deploy V2\Msdeploy.exe -verb:sync -source:contentPath=c:\source Path -dest:contentPath=\\unc path to a remoted server\folderso, it stores necessary information to run MSdeploy with the necessary parameters I want to have.
But since it contains C:\Program Files\IIS\Microsoft Web Deploy V2\msdeploy.exe,  I get the error message, "cmd.exe : 'C:\Program' is not recognized as an internal or external command". 
I am kinda under why the error happens but I can't seem to find a better answer yet. cmd.exe runs from c:\windows\system32\ and then the created script run c:\program files\IIS\Microsoft Web Deploy V2\Msdeploy.exe from c:\windows\system32 directory.
This is where I'm trying to figure out.
How can I run this without putting $runMSDeploy into a batch file? Is there any cmlet I can use to run $runMSDeploy? Invoke-command or Invoke-Expression?
Top 200 Contributor
Posts 9

Hi, you just need to get youre quotations right so that the spaces in the path to the executable are catered for.

I notice that you are trying to force the location to the executables directory, this is a good way to approach it if you are having big problems with quotations. For reference the escape character in cmd is ^ and in PS is `.

You could use invoke-expression, the following should help you out. Note the & character in the expression string I build, this is an alias to invoke-expression, I have found that doubling up the invoke-expression allows me to easily build a string with quotations etc and execute it without problems.  Another thing worthy of note is that if you are defining a string variable and want to expand the content of a variable within ' characters you must wrap the variable in $() otherwise they will be left unexpanded in your final string -

 

# --- Test external exe is available
$Msdeploy  = "C:\Program Files\IIS\Microsoft Web Deploy V2\msdeploy.exe"
if (!(Test-Path -Path $MsDeploy))
{
    throw "Unable to locate msdeploy executable
    break
}

# Build the expression, cater for spaces in exe path

$Expression = "& '$($MsDeploy)' -verb:sync -source:contentPath='$($buildDroppedLocation)' -dest:contentPath='$($destinationPath)' -whatif | Set-Content -Path C:\temp\msdeploy.log"

# --- invoke the expression and check return code
Invoke-Expression $Expression
if ($LASTEXITCODE -ne 0)
{

     Write-Host "An error occurred during invocation of msdeploy, please review log : C:\temp\msdeploy.log" -ForeGroundColor "Red"
}
else
{
     Write-Host "Success" -ForeGroundColor "Green"
}

 

 

Page 1 of 1 (2 items) | RSS
Copyright 2012 PowerShell.com. All rights reserved.