The last couple of weeks I’m quite busy with 2 projects. For both projects I have written a PowerShell script for deploying the solutions and provisioning the SharePoint sites. In these series of PowerShell posts I‘d like to share my PowerShell scripts.
For starters, the scripts are not just for the final deployment, but they are very useful and time saving (!) during development and test. How many times did you start over again deleting and creating the site collection, activate one of more features to test and create a web site along with some lists, etc… ? You do not (I repeat YOU DO NOT) want that to do it many times a day. PowerShell to the rescue!
So, let’s begin. We start with 2 scripts, one for deploying and one for removing your solution. I use PowerGUI as the tool for writing and debugging PowerShell Scripts. I cannot live without it anymore.
The Deploy.ps1 script file contains the following code:
param([string]$url="http://intranet.octavie.local") $solutionName = "Octavie.SharePoint.Intranet.wsp" $solutionFile = ".$solutionName" $solution = Add-SPSolution $solutionFile if ( $solution.ContainsWebApplicationResource ) { Write-Host -ForegroundColor Green "Deploying $solutionName to $url" Install-SPSolution -Identity $solutionName -GacDeployment -CasPolicies -Force -Webapplication $url } else { Write-Host -ForegroundColor Green "Deploying $solutionName" Install-SPSolution -Identity $solutionName -GacDeployment -CasPolicies -Force } $solution = Get-SPSolution $solutionName if ($solution.Deployed -eq $false ) { $counter = 1 $maximum = 50 $sleeptime = 2 while( ($solution.JobExists -eq $true ) -and ( $counter -lt $maximum ) ) { write-host -ForegroundColor yellow "Please wait..." sleep $sleeptime $counter++ } } Write-Host "" Write-Host -ForegroundColor Green "$solutionName is deployed"
Three important cmdlets are used here:
- Add-SPSolution : this adds your WSP to the Farm’s Solutions
- Install-SPSolution: this deploys your solution to one or more web applications
- Get-SPSolution : this gets a SPSolution object, so we can check if the solution is already deployed. Hence, the Deployed and JobExists property.
The Remove.ps1 script file contains the following code:
param([string]$url="http://intranet.octavie.local") $solutionName = "Octavie.SharePoint.Intranet.wsp" $solution = Get-SPSolution $solutionName Write-Host -ForegroundColor Green "Removing $solutionName" Write-Host "" if( $solution.Deployed -eq $true) { if ( $solution.ContainsWebApplicationResource ) { Uninstall-SPSolution -Identity $solutionName -Confirm:$false -Webapplication $url } else { Uninstall-SPSolution -Identity $solutionName -Confirm:$false } $counter = 1 $maximum = 50 $sleeptime = 2 while( $solution.JobExists -and ( $counter -lt $maximum ) ) { Write-Host -ForegroundColor yellow "Retracting $solutionName. Please wait..." sleep $sleeptime $counter++ } } Write-Host "" Write-Host -ForegroundColor Green "$solutionName is retracted." Write-Host "" Remove-SPSolution -Identity $solutionName -Force -Confirm:$false Write-Host -ForegroundColor Green "$solutionName removed."
In the next post I will cover creating a site collection and provisioining it with some content. Then I wil also use a refactored script for deploying and removing solutions.
Very nice .. just what I needed!