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.
 

Ingen kommentarer:

Send en kommentar