Tuesday, May 10, 2011

Poweshell script to install and activate Feature in SharePoint 2010

People who say, Boss I am fed up typing this powershell / STSADM command to Install the features... can we make it easy somehow ???

Yes buddy, here you go... Copy the below script and your done!

################################################################################################################################################
# This Powershell script helps you to install and activate features for
# your webapplication / site collection
# If the feature already exist then this will deactivate, uninstall then
# Install and enable, based on your requirment
# Created By Kannaganesh, dated 03-May-11
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++

# remove "#" in the below line, if you are not running this script in
# SharePoint Shell.This will install Powershll snap-ins for Sharepoint
# add-pssnapin microsoft.sharepoint.powershell

# Below line reads your keyboard input for feature folder name

$FolderName = read-host "FeatureFolderName"

# Below line reads ur keyboard input for URL

$URL = read-host "URL"

# Next two lines check for the availablity of the feature which you are about to install

$getfeature = Get-SPfeature | where-object {$_.DisplayName -eq "$foldername"}
$getfeature
If ($getfeature -eq $null)

# Below two lines installs the feature if it is not already installed.

{Install-SPFeature $folderName -force;
Enable-SPFeature $FolderName -url $URL;
write-output "$folderName is installed and enabled for $url" }

# If Yes the following else statment will disable, uninstall then Install # and enable the feature to your site

else

{disable-spfeature -identity $foldername -url $url ;
start-sleep -s 5 ;
uninstall-spfeature -identity $foldername;
start-sleep -s 5 ;
Install-SPFeature $folderName -force;
start-sleep -s 5 ;
Enable-SPFeature $FolderName -url $URL ;
write-output "$folderName is re-installed and enabled for $url"
}

Consideration

1) The feature folder is available on all the servers in your farm.
2) The feature your trying to install / activate is a farm based and not server based.
3) If it is server based, this needs to be executed on all servers in farm.

Procedure for executing this script

I am sure, most of you know, how to execute this script. For folks, who need assistance follow the below steps.

1) Copy the above script to a notepad and save it as Installfeature.ps1 (you can use your defined file name with ext .ps1). Ensure, you elimenate space or extra "#" in the copied script, if any.

2) Open SharePoint Shell as Administrator.

3) Navigate to the folder, where your script is placed.

4) Now its time to execute, .\installfeature.ps1 (this can be easily achieved by .\In and a tab).

5) You will be prompted for folder name and URl values one after other.

6) What next, if your feature is not existing. It silently installs and "tells you BOSS your done !"

7) If It is existing , then you will be prompted for your approval to continue / cancel.

Hope this helps...

Disclaimer :-

This script is tested in few env and found to be working, please try the same in your dev/test env before moving in to production.

Good luck :)

No comments:

Post a Comment