3

Oh great powershell gods, please show this heathen the light!

I have been tasked with creating a 1 click publish-our-solution-to-the-cloud powershell script. I have, however, never used PS in my life. I have therefore been doing quite a bit of Googling and found a good bit of information however where I want to start I'm having a little difficulty finding something I can wrap my head around.

I keep running across pre/post build scripts, VS management console, nuget package management or any other number of seemingly unrelated info. These are not what I'm looking for.

Either by example here or linking to a good tutorial to get me started. My first baby step is to get VS to package the solution for release. Do the below action via PS.

Image for clarity, just in case...

I thought this would be the easiest to find info on... I have seen some info on MSBuild which I'm looking into now as I hopefully get an answer here.

EDIT: My working PS Script

$msbuild = "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\msbuild.exe" # Reference to the MSBuild.exe for .NET 4.0
$path = "C:\myProj\"                                  # Path to the project you want to publish to Azure 

# Clean the solution
& $msbuild ($path + "Source\pathToYour\AzureMyProj.ccproj") /target:clean /p:Configuration=Release

# Package the solution
& $msbuild ($path + "Source\pathToYour\AzureMyProj.ccproj") /target:publish /p:Configuration=Release
meshy
  • 8,470
  • 9
  • 51
  • 73
Justin williams
  • 574
  • 1
  • 8
  • 26

2 Answers2

4

you can use MSBuild for this purpose. This is how I do it for my projects:

So from powershell, you could call it like this:

.\MSBuild MyProject.proj /target:package /p:Configuration=Release

EDIT 25/06: Worked for James with the /target:publish

Note: You may need to specify the location of MSBuild as a variable, this can be found under C:\Windows\Microsoft.Net\Framework\versionNumber\

In that case it'd become:

$msbuild = "C:\Windows\Microsoft.NET\Framework64\v2.0.50727\msbuild.exe"
& $msbuild MyProject.proj /target:package /p:Configuration=Release

Notice the & instead of the .\ this tells powershell to execute the variable (which in this case is an exe).

Also, further to your problem of not really knowing how to do it. I use a build language called PSake which is a PowerShell based build language which may be of use to you. It's pretty useful: https://github.com/psake/psake

Hope this helps.

Kyle Muir
  • 3,875
  • 2
  • 22
  • 27
  • Thanks, I'll try this out later today when I get a free minute. – Justin williams Jun 19 '13 at 15:59
  • I'm getting an error: AzureWebOnly.ccproj : error MSB4057: The target "package" does not exist in the project. If i use /target:build it builds and it's looking like I get my .cscfg and .csdef files so hopefully that's enough. – Justin williams Jun 19 '13 at 18:21
  • Ahh, damn. I deal primarily with SharePoint CI so the package target builds us a nice WSP which we can deploy. Perhaps this question can assist you further? http://stackoverflow.com/questions/3097489/how-to-publish-web-with-msbuild – Kyle Muir Jun 19 '13 at 19:30
  • Yeah, I just noticed it's missing the .cspkg file which is -kinda- important... :p I get the config files and can do what I need with those so this was still a really good start. I'll take a look at the question you linked. – Justin williams Jun 19 '13 at 21:37
  • I think it has something to do with it being a .ccproj rather than a .csproj – Justin williams Jun 19 '13 at 22:27
  • 1
    For those unfortunate ones still stuck on XP (with previous versions of VS) just replace the `$msbuild` assignment with `$msbuild = "$env:WINDIR\Microsoft.NET\Framework\v4.0.30319\msbuild.exe"` (for .NET 4). Windows lives in a different folder on this archaic system.. – karel_evzen Jun 24 '13 at 09:18
  • Forgot to update this. The package works with "/target:publish" – Justin williams Jun 24 '13 at 21:47
  • Updated to reflect your fix :) – Kyle Muir Jun 24 '13 at 22:07
1

For Web Deploy 3 I had a few problems separating the arguments correctly. From a template MVC4 application the following worked for me.

$msbuild = "C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe"
$buildCommand = "& $msbuild MvcApplication.csproj '/target:package' /P:Configuration=Release"

Write-Host
Write-Host "[$scriptName] $buildCommand"
Invoke-Expression $buildCommand
$exitcode = $LASTEXITCODE
if ( $exitcode -gt 0 ) { 
    Write-Host
    Write-Host "[$scriptName] Build using Web Deploy failed with exit code = $exitcode" -ForegroundColor Red
    throw "Build using Web Deploy failed with exit code = $exitcode" 
}
Jules Clements
  • 418
  • 5
  • 8