Sunday 8 May 2011

PowerShell script to create batch release files for SharePoint wsp files

If you have a heavily customised SharePoint installation that is under continuous development here is a quick way to use PowerShell to generate batch files you can hand over to your deployment team alongside the wsp files. If your process consist of managed releases to multiple environments this might be a time saver. As with anything there are many ways to skin a cat, and this is just one example of how to script this repetitive task into a simple drag, drop and click.


The PowerShell script relies on the wsp’s being in a folder structure similar to this:







If this doesn’t suit your requirements simply change the bits in the script that sets the $targetEnvironment variable, either get it from some other environment variable you can use or pass a parameter to the script.

$WhatIfPreference = 0

<#
creates files 01 - Retract.bat, 02 - Delete.bat, 03 - Add.bat, 04 - Deploy.bat.
For each wsp found and matched the relevant stsadm command is added to the files.
Drop this file into the release folder where you want to create the bat files.
#>

$devUrl = "http://vs.dev.intra.sample.org"
$testUrl = "http://vs.test.intra.sample.org"
$liveUrl = "http://vs.intra.sample.org"
$uatUrl = "http://vs.uat.intra.sample.org"
$execadmjobs = "stsadm -o execadmsvcjobs"

#set working directory to current execution dir
$invocation = (Get-Variable MyInvocation -Scope 0).Value
Set-Location (Split-Path $invocation.MyCommand.Path)

#target environment is picked up from the execution path
$targetEnvironment = "dev" 
if($invocation.MyCommand.Path.Contains("dev"))
{
    $targetEnvironment = "dev";
}
if($invocation.MyCommand.Path.Contains("test"))
{
    $targetEnvironment = "test";
}
if($invocation.MyCommand.Path.Contains("live"))
{
    $targetEnvironment = "live";
}
if($invocation.MyCommand.Path.Contains("uat"))
{
    $targetEnvironment = "uat";
}

#Retract values
$retractors = @{
"VS.SomeFeature.wsp" = "stsadm -o retractsolution -name VS.SomeFeature.wsp -immediate";
"VS.SomeOtherFeature.wsp" = "stsadm -o retractsolution -nameVS.SomeOtherFeature.wsp -allcontenturls -immediate ";
}

#Delete values
$deleters = @{
"VS.SomeFeature.wsp" = "stsadm -o deletesolution -name VS.SomeFeature.wsp";
"VS.SomeOtherFeature.wsp" = "stsadm -o deletesolution -name VS.SomeOtherFeature.wsp";
}

#Add values
$adders = @{
"VS.SomeFeature.wsp" = "stsadm -o addsolution -filename VS.SomeFeature.wsp";
"VS.SomeOtherFeature.wsp" = "stsadm -o addsolution -filename VS.SomeOtherFeature.wsp";
}

#Deploy values
$deployers = @{
"VS.SomeFeature.wsp" = "stsadm -o deploysolution -name VS.SomeFeature.wsp -immediate -allowGacDeployment -force";
"VS.SomeOtherFeature.wsp" = "stsadm -o deploysolution -name VS.SomeOtherFeature.wsp -url URLTOREPLACE -immediate -allowGacDeployment -force";
}

New-Item -name "01 - Retract.bat" -type file
New-Item -name "02 - Delete.bat" -type file
New-Item -name "03 - Add.bat" -type file
New-Item -name "04 - Deploy.bat" -type file

Get-ChildItem -name -include *.wsp | foreach-object -process {out-file -filepath "01 - Retract.bat" -append -encoding Ascii -inputobject $retractors[$_]} 
Get-ChildItem -name -include *.wsp | foreach-object -process {out-file -filepath "02 - Delete.bat" -append -encoding Ascii -inputobject $deleters[$_]} 
Get-ChildItem -name -include *.wsp | foreach-object -process {out-file -filepath "03 - Add.bat" -append -encoding Ascii -inputobject $adders[$_]} 
Get-ChildItem -name -include *.wsp | foreach-object -process {out-file -filepath "04 - Deploy.bat" -append -encoding Ascii -inputobject $deployers[$_]} 

Add-Content -encoding Ascii -path "01 - Retract.bat" -value $execadmjobs
Add-Content -encoding Ascii -path "04 - Deploy.bat" -value $execadmjobs
Add-Content -encoding Ascii -path "01 - Retract.bat" -value "pause"
Add-Content -encoding Ascii -path "02 - Delete.bat" -value "pause"
Add-Content -encoding Ascii -path "03 - Add.bat" -value "pause"
Add-Content -encoding Ascii -path "04 - Deploy.bat" -value "pause"

switch($targetEnvironment){
 "dev" {(get-content "04 - Deploy.bat") | foreach-object {$_ -replace "URLTOREPLACE", $devUrl} | set-content "04 - Deploy.bat";break}
 "test" {(get-content "04 - Deploy.bat") | foreach-object {$_ -replace "URLTOREPLACE", $testUrl} | set-content "04 - Deploy.bat";break}
 "live" {(get-content "04 - Deploy.bat") | foreach-object {$_ -replace "URLTOREPLACE", $liveUrl} | set-content "04 - Deploy.bat";break}
 "uat" {(get-content "04 - Deploy.bat") | foreach-object {$_ -replace "URLTOREPLACE", $uatUrl} | set-content "04 - Deploy.bat";break}
}

$WhatIfPreference = 0
To use it just add the relevant stsadm commands for your wsp’s to the arrays $retractors, $deleters, $adders and $deployers in this format:
"WspName.wsp" = "stsadm command";
Lines 62 – 65 creates the new .bat files in the current directory.
Lines 67 – 70 iterates over all .wsp files it finds in the current directory and adds the relevant stsadm command to the .bat files if a match is found in the arrays.
Lines 79 – 83 replaces any occurrence of [URL] in the commands with the relevant url for the target environment.
If you have 20-30 wsp’s this can be a real time saver as now all you have to do to create the batch files is to drop the wsp's for your current release into a folder alongside the script and execute it. On a 2003 app server you can just double click each batch file in order for the actual SharePoint deployment, on a 2008 server you might have to run a command prompt as Administrator and execute the batch files through that.

The script can be downloaded here.

No comments:

Post a Comment