20 June 2011

PowerShell SharePoint WebPart deployment Script

I think everyone is creating either Visual WebParts or Sandbox Solutions for SharePoint 2010 using Visual Studio 2010 project template. Won’t it be nice if the project includes powershell deployment scripts as one of the artifacts when you create a project. As MS left that on us to do that, I am posting the scritps that I am using for install and uninstall of VisualWebPart Solution to a Web Application and activating it at “Web” scope.

I have created a batch file to kickoff the poweshell script. it looks like this:

ET LaunchedFromBAT=1
IF EXIST "%~dp0\install.ps1" (
ECHO - Using standard Input File.
GOTO START
)
ECHO - Input File not found! Please check for install.ps1, AutoSPInstallerInput
GOTO END
:START
:: Check for Powershell
IF NOT EXIST "%SYSTEMROOT%\system32\windowspowershell\v1.0\powershell.exe" (
COLOR 0C
ECHO - "powershell.exe" not found!
ECHO - This script requires PowerShell - install v2.0, then re-run this script.
COLOR
pause
EXIT
)
:: Check for Powershell v2.0
ECHO - Checking for Powershell 2.0...
"%SYSTEMROOT%\system32\windowspowershell\v1.0\powershell.exe" $host.Version.Major | find "2" >nul
IF ERRORLEVEL 1 (
COLOR 0C
ECHO - This script requires PowerShell version 2.0!
ECHO - Please uninstall v1.0, install v2.0, then re-run this script.
COLOR
pause
EXIT
)
ECHO - OK.
:: Get existing Powershell ExecutionPolicy
FOR /F "tokens=*" %%x in ('"%SYSTEMROOT%\system32\windowspowershell\v1.0\powershell.exe" Get-ExecutionPolicy') do (set ExecutionPolicy=%%x)
:: Set Bypass, in case we are running over a net share or UNC
IF NOT "%ExecutionPolicy%"=="Bypass" IF NOT "%ExecutionPolicy%"=="Unrestricted" (
ECHO - PS ExecutionPolicy is %ExecutionPolicy%, setting ExecutionPolicy to Bypass.
"%SYSTEMROOT%\system32\windowspowershell\v1.0\powershell.exe" -Command Start-Process "$PSHOME\powershell.exe" -Verb RunAs -ArgumentList "'-Command Set-ExecutionPolicy Bypass'"
)
GOTO LAUNCHSCRIPT
:LAUNCHSCRIPT
"%SYSTEMROOT%\system32\windowspowershell\v1.0\powershell.exe" ".\install.ps1"
GOTO END



Installation script:



#Author: Sumit Rawat
#Script: install SampleSolution

#Load SharePoint Snapin
Add-PSSnapin Microsoft.SharePoint.PowerShell

#Set up Web Application variable
#Only needed if solution contains Web Application scoped resources
$webApp = Get-SPWebApplication -Identity $Args[0]

echo "Adding Solution"
#Add solution file to the farm
$solution = Add-SPSolution -LiteralPath "C:\SampleSolution.wsp"

echo "Deploying Solution"
#Deploy solution
#Add -WebApplication $webApp to the line below if solution contains Web Application scoped resources
Install-SPSolution $solution -WebApplication $webApp -Force -GACDeployment

#Wait for solution to be deployed
do {Start-Sleep -s 1} while ($solution.Deployed -eq $false)

#Install Feature
echo "Installing Feature"
Install-SPFeature "SampleFeature" -force

echo "Activating Feature"
#Activate web scoped feature in one site
$web = Get-SPWeb $Args[0]
Enable-SPFeature -Identity "SampleFeature" -Url $web.Url
$web.Dispose()
echo "Installation Complete....."



Uninstall script:



#Author: Sumit Rawat
#Script: Uninstall SampleSolution

#Load SharePoint Snapin
Add-PSSnapin Microsoft.SharePoint.PowerShell

#Set up Web Application variable. Only needed if solution contains Web Application scoped resources
$webApp = Get-SPWebApplication -Identity $Args[0]

echo "Disabling Feature"
#Deactivate web scoped feature in one site
$web = Get-SPWeb $Args[0]
Disable-SPFeature -Identity "SampleFeature" -Url $web.Url
$web.Dispose()

echo "Uninstalling Feature"
#UnInstall Feature
UnInstall-SPFeature -Identity "SampleFeature" -force

echo "Retracting solution"
#Add -WebApplication $webApp to the line below if solution contains Web Application scoped resources
UnInstall-SPSolution -Identity "SampleSolution.wsp" -WebApplication $webApp

#Wait for solution to be deployed
do {Start-Sleep -s 20} while ($solution.Deployed -eq $true)

echo "removing solution"
#Remove solution file to the farm
Remove-SPSolution -Identity "SampleSolution.wsp" -force

echo "uninstall complete...."

0 comments: