mandag den 17. december 2012

How to get feature scope using powershell

As i promissed in last post, here is a quick example for a easy way to get the feature scope, when you want to enable a Sharepoint feature.

I added two small functions that can activate a feature based its scope. this way you dont have to specify the feature scope in the script and this way you only need one function for all your features. This function will work fine with my lates post on how to specify all the features in an xml file.

This function takes two parameters: The Identety of the feature, and the URL for a given site. If the url is empty it is set to $null.


-------------------------------------
function ActivateFeature
{    param(
         $Identity,
         $Url = $null
         )
if ($installedFeature = checkFeatureInstalledAndScope -FeatureID $Identity)

{    switch ($installedFeature.Scope)
   {    
    "Farm"
     {
       $feature = (Get-SPFeature -Identity $Identity -Farm
       -erroraction silentlycontinue)
     }        
    "WebApplication"

     {
       $feature = (Get-SPFeature -Identity $Identity -WebApplication $Url
       -erroraction silentlycontinue)
     }
    "Site"
     {
        $feature = (Get-SPFeature -Identity $Identity -Site $Url
        -erroraction silentlycontinue)
     }
    "Web"
     {
        $feature = (Get-SPFeature -Identity $Identity -Web $Url
        -erroraction silentlycontinue)
     }
   }
 
Write-Host "Enabling feature: " $installedFeature.Id "..." -NoNewline

if ($installedFeature.scope -eq "Farm")
{
   Enable-SPFeature -Identity
$Identity

}
else
{
   Enable-SPFeature -Identity
$Identity -Url $Url

}  
Write-Host -ForegroundColor Green "DONE"

function checkFeatureInstalledAndScope
{
 
param(
$FeatureID)

  if ($feature = (get-spfarm).FeatureDefinitions | ?{$_.Id -eq $FeatureID})
  {
    return $feature
  }

  return $null
}

onsdag den 12. december 2012

Activate features using XML file

I have worked with a smal powershell script that activate a lot of features specified in a XML file.

I was strucling with a script that had all features hardcoded in the script, and I thought to myself, that there should be a better way to du this! More stuctured! Easyer to maintain!

The way to activate a feature using powershell is well known.
 - Enable-SPFeature -Identity "Identity"
Look here for more information on the Enable-SPFeature command
http://technet.microsoft.com/en-us/library/ff607803.aspx

So how can we then make this more structured. Well my idear is to use a xml file to hold all the features that need enables. his also makes the maitanece more simple.
For this scrip I use true/false to activate features in the xml file.

This is an example on the xml structure I use.
<?xml version="1.0" encoding="utf-8"?>
<DeploymentConfig>
   <Features Name="Farm">

      <Feature Enable="true" Scope="Farm" Identity=""></Feature>
   </Features>
</DeploymentConfig>

So how to import the xml file in the script.
$dp0 = the location of the script.

$featureXMLFile = $dp0 + "\xx.xml"
[xml]$FeatureConfig = [xml](Get-Content $featureXMLFile)


Using this xml file in a scrip to find the features that need activating.

$featuresEnabled = $FeatureConfig.SelectNodes
#Finding features set to be enabled (Enable="true")
("DeploymentConfig/Features/Feature") | where {$_.Enable -ieq "true"}
 

if ($featuresEnabled)
{
  Write-Host -ForegroundColor Green "--------------------------"
  Write-Host -ForegroundColor Green "- Activating Features"
 

  #Farm scoped features
  $farmFeatures = $featuresEnabled | where {$_.Scope -eq 'Farm'}
  if ($farmFeatures)
  {
    Write-Host -ForegroundColor Green " - Enabling Farm scoped feature:"
    foreach ($feature in $farmFeatures)
    {
       
$FeatureUID = $feature.Identity
       
Enable-SPFeature -Identity $FeatureUID
    }
  }
  else
  {
    Write-Warning "No Farm features to activate"
  }

}

This little script only handles farm features. To use it for weballication/sitecollection features you need to add a webapplication/site to the command.
Enable-SPFeature -Identity $FeatureUID -Url $Url

To make this possible you need to create a new section in the xml file that specify the features that need activation. in this example I use webapplication.
<Features Name="WebApplication">
  <Feature Enable="false" Scope="WebApp" Site="" Identity=""></Feature>
</Features>


The code should look like this.
$webappFeatures = $featuresEnabled | where {$_.Scope -eq 'WebApp'}
if ($webappFeatures )
{
  Write-Host -ForegroundColor Green " - Enabling webapplication scoped feature:"
  foreach ($feature in $webappFeatures )

  {
    $FeatureUID = $feature.Identity

    $Url = $feature.Site
    Enable-SPFeature -Identity $FeatureUID -Url $Url 

  }
}

You can of course build this into a larger script, and I will in a later post discripe how to find out if a feature is farm, webapp, site or web.

Have fun with powershell.