Deployment of wsp solutions to a productive SharePoint farm is still a complex task (although since Visual Studio 2010 deployment steps for local deployment). But with the SPSF SharePoint Software Factory (free download at http://spsf.codeplex.com) the automated deployment of wsp solutions becomes very easy.
With SPSF developers can create SharePoint projects in Visual Studio and a separate project for deployment of the solution files is added automatically. The project “ApplicationDeployment” provides msbuild files which allow the deployment of SharePoint solutions to a SharePoint farm per script.
Today I want to describe, how Powershell scripts (.ps1) can be executed during the deployment process in SPSF. This can be useful e.g. to activate several features, to create search properties or anything else which can be done with Powershell.
For testing I used a simple script and called it Script.ps1
$site=Get-SPSite “http://demo2010a”
write-output “Log message: Output of Powershell”
write-output $site.Url
This script simply requests a SPSite and outputs the URL.
1. Step: Add Powershell script to the SharePoint project in Visual Studio
The .ps1 files should be added to the project “ApplicationDeployment” in folder “DeploymentFiles”. Each file in this folder will be copied to the installation package during build. I would recommend to create a subfolder “PowerShell” to store all .ps1 files.
2. Add call to .ps1 file to the “DeploymentTargets.msbuild”
The project “ApplicationDeployment” contains a file called “DeploymentTargets.msbuild”. This file contains several targets which are automatically called during the deployment process. There are “BeforeDeploy” and “AfterDeploy“, “BeforeUpgrade” and “AfterUpgrade” and “BeforeUndeploy” and “AfterUndeploy“. These targets can contain calls to stsadm or powershell to execute actions at a special point during deployment. They can be used to activate and deactivate features, create sites etc.
To run a .ps1 Powershell script you only need to add the following statement to a target.
<Target Name=”AfterDeploy”>
<!– call to custom .ps1 file which should be execute after deployment –>
<Exec Command=”$(PS) -file "Powershell\Script.ps1"” />
</Target>
The call to EXEC in msbuild runs a command line program, in this case it is Powershell.
Why should you use this approach?
The created package for the deployment writes all output of the deployment to a single logfile (Deployment.log). This is important to trace errors and warnings during a deployment.
The advantage of calling .ps1 files with msbuild is that the output of the Powershell script is written to the same lofile of msbuild and no separate logfiles must be written in the code of .ps1 files. It makes it easier to check the deployment for errors and warnings.
Alternative
You can also execute Powershell commands directly in the DeploymentTargets.msbuild. Here are some samples:
<Exec Command=”"$(STSADM)" -o activatefeature -name ‘Feature1′ -url ‘http://demo201a’” /><!– Sample PowerShell commands
<Exec Command=”$(PS) -command ". New-SPSite -Url ‘http://demo201a’ -OwnerAlias ‘CONTOSO\Administrator’ -Name ‘Test Site’ -Template ‘STS#0′"” />
<Exec Command=”$(PS) -command ". Install-SPFeature -Path ‘Feature1′ -Force"” />
<Exec Command=”$(PS) -command ". Enable-SPFeature -Identity ‘Feature1′ -Url ‘http://demo201a’ -Force"” />
</Target>









