PowerShell Script To Get Custom Feature Status From Web Application

Objective

This purpose of this document is to get custom feature status (Activated/Deactivated) from selected sets of site collection configured in XML file using PowerShell script. The reusable script for this job has also attached with this document. This script offers,
  • To read a set of site collections, from which you want to get the feature status. 
  • Generate a report in excel format which will provide all the site details and feature status info.
Business Case

S. No.Business Case
1For several reasons we SharePoint Developer/administrators want to check if the features are activated for site collections or not. Consider a case where after the deployment administrator want to check if this feature is been activated on site collection (say 10 site collection), manually going to every managefeatures.aspx page for site and web level is not feasible. Doing like this one by one is a painful job for administrator/developer. This script will allow administrator/developers to do the job just by configuring the XML file.

Targeted Audience
  • SharePoint Application Developers
  • SharePoint Administrator
  • SharePoint Architect
Offerings
  • One reusable PowerShell script is provided which needs to be run to get the feature status for selected set of site collections.
  • Format of XML file is provided where site collection needs to be configured.
Technical Details

Below are the technical details for this PowerShell script,

1: Pre-requisites
  1. Login to server with Farm administrator account and copy the folder and paste the same to the location where you want to keep it.
  2. Open the folder and Configure XML file as per your requirement.
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <Configuration Environment="DEV" Version="1.0.0.0">  
  3.   <GlobalWebApplications>  
  4.     <GlobalWebApplication url="http://mywebapplicationURL">    
  5.      <CustomFeatures>  
  6.     <SiteScoped>  
  7.             <Feature id="xxxxx-xxx-xxxx-xxxx-xxxxxxxxxxx">  
  8.                <Name>XYZ Site Columns Feature</Name>  
  9.             </Feature>  
  10.         <Feature id="xxxxx-xxx-xxxx-xxxx-xxxxxxxxxxx">  
  11.                <Name>XYZ Content Types Feature</Name>  
  12.             </Feature>  
  13.         <Feature id="xxxxx-xxx-xxxx-xxxx-xxxxxxxxxxx">  
  14.                <Name>XYZ Feature</Name>  
  15.             </Feature>  
  16.     </SiteScoped>  
  17.     <WebScoped>  
  18.         <Feature id="xxxxx-xxx-xxxx-xxxx-xxxxxxxxxxx">  
  19.                <Name>XYZ Site Lists Feature</Name>  
  20.             </Feature>  
  21.             <Feature id="xxxxx-xxx-xxxx-xxxx-xxxxxxxxxxx">  
  22.                <Name>XYZ Event Receivers Feature</Name>  
  23.             </Feature>  
  24.     </WebScoped>  
  25.     </CustomFeatures>  
  26.    </GlobalWebApplication>  
  27.   </GlobalWebApplications>  
  28. </Configuration>  
As shown in the below image; enter Web Application URL at https://myWebApplicationURL , configure a set of features at site as well as web level as shown in in above image. 

Note: Name the file as Configuration.xml only.

2: Execution 

Prerequisite:

Login to SharePoint Server as Farm Administrator and copy the required files (PowerShell script and configuration XML).Configure the XML file as per your requirement.

Run:
  1. Run the PowerShell Script as “Run as Administrator“.
  2. Browse the folder path where you have kept this PowerShell script file and execute a command as shown in below image.
Note:
  1. As shown in above image; Script will ask you for a path where you want to keep these backup file

  2. In case you have typed site collection URL wrongly this script will show you an error in red color asking you to check if you have typed the URL correctly. 

    error
3: Output Folder
  1. Please find below folder location which we have configured in our script to maintain a site collection backup output location.

  2. Output will be csv file with name as GetFeatureStatusResults, will contain information like,

    a. Site Template
    b. Site URL
    c. Site Creation Date
    d. Site collection level Feature status
    e. Web Level Feature status
PowerShell Script
  1. #check to see if the PowerShell Snapin is added  
  2. if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {  
  3.     Add-PSSnapin Microsoft.SharePoint.PowerShell;  
  4. }  
  5.  
  6. ## SharePoint DLL   
  7. [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")   
  8. $global:currentPhysicalPath = Split-Path ((Get-Variable MyInvocation -Scope 0).Value).MyCommand.Path  
  9. [xml]$xmlinput = (Get-Content "$global:currentPhysicalPath\Configuration.xml")  
  10.    
  11. Function Get-SPWebApplication()  
  12. {    
  13.   Param( [Parameter(Mandatory=$true)] [string]$WebAppURL )  
  14.   return [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($WebAppURL)  
  15. }  
  16. #EndRegion  
  17.  
  18. #Region Check Site Level Custom Features Status  
  19. # ===================================================================================  
  20. # Func: Check Site Level Custom Features status  
  21. # Desc: Check custom features status across multiple site collections  
  22. # ===================================================================================  
  23. function Global_CheckSiteLevelFeatureStatus([System.Xml.XmlElement]$feature, [String]$url)  
  24. {  
  25.   $identity = $feature.id   
  26.   $featureName = $feature.Name  
  27.    
  28.   $featureObject = Get-SPFeature -Site $url | Where-Object { $_.id -eq $identity }  
  29.   
  30.   if ($featureObject -eq $null)  
  31.   {  
  32.     return $url  
  33.   }  
  34. }  
  35.  
  36. #Region Check Web Level Custom Features Status  
  37. # ===================================================================================  
  38. # Func: Check Web Level Custom Features status  
  39. # Desc: Check custom features status across multiple site collections  
  40. # ===================================================================================  
  41. function Global_CheckWebLevelFeatureStatus([System.Xml.XmlElement]$feature, [String]$url)  
  42. {  
  43.   $identity = $feature.id   
  44.   $featureName = $feature.Name  
  45.    
  46.   $featureObject = Get-SPFeature -Web $url | Where-Object {$_.id -eq $identity}  
  47.   
  48.   if ($featureObject -eq $null)  
  49.   {  
  50.     return $url  
  51.   }  
  52. }  
  53. #EndRegion  
  54.  
  55. #Region Check Custom Features Status  
  56. # ===================================================================================  
  57. # Func: Global_GetSiteCollectionFeatureStatus  
  58. # Desc: Check custom features status across multiple site collections      
  59. # ===================================================================================  
  60. function Global_GetSiteCollectionFeatureStatus([xml]$xmlinput)  
  61. {  
  62.    foreach($configWebApp in $xmlinput.Configuration.GlobalWebApplications.GlobalWebApplication)  
  63.    {  
  64.         $webApp = Get-SPWebApplication($configWebApp.url)   
  65.         if($webApp -eq $null)  
  66.         {   
  67.             Write-host Web Application at url : $configWebApp.url does not Exists.. -foregroundcolor Red  
  68.         }  
  69.         else  
  70.         {  
  71.             Write-host Web Application at url : $configWebApp.url Exists.... -foregroundcolor Green  
  72.             $results = @()  
  73.               
  74.             foreach($siteCollection in $webApp.Sites)  
  75.             {  
  76.                 if($siteCollection -ne $null)  
  77.                 {   
  78.                      #Create an object to hold storage data  
  79.                     $resultsData = New-Object PSObject  
  80.                     $resultsData | Add-Member -type NoteProperty -name "Site URL" -value $siteCollection.Url -Force  
  81.                     $resultsData | Add-Member -type NoteProperty -name "Site Template" -value $siteCollection.rootweb.WebTemplate -Force  
  82.                     $resultsData | Add-Member -type NoteProperty -name "Site Created Date" -value $siteCollection.rootweb.Created.ToShortDateString() -Force  
  83.                     ForEach($feature in $xmlinput.Configuration.GlobalWebApplications.GlobalWebApplication.CustomFeatures.SiteScoped.Feature)  
  84.                     {  
  85.                        $siteFeatureObject = Get-SPFeature -Site $siteCollection.Url | Where-Object { $_.id -eq $feature.id }  
  86.                        if ($siteFeatureObject -eq $null)  
  87.                        {  
  88.                            $resultsData | Add-Member -type NoteProperty -name $feature.Name -value "NO" -Force    
  89.                        }  
  90.                        else  
  91.                        {  
  92.                            $resultsData | Add-Member -type NoteProperty -name $feature.Name -value "YES" -Force     
  93.                        }  
  94.                     }  
  95.                     if($siteCollection.AllWebs.Count -gt 0)  
  96.                     {  
  97.                         ForEach($web in $siteCollection.AllWebs)  
  98.                         {  
  99.                             $resultsData | Add-Member -type NoteProperty -name "Site URL" -value $web.Url -Force  
  100.                             ForEach($feature in $xmlinput.Configuration.GlobalWebApplications.GlobalWebApplication.CustomFeatures.WebScoped.Feature)  
  101.                             {  
  102.                                 $webFeatureObject = Get-SPFeature -Web $web.Url | Where-Object {$_.id -eq  $feature.id}  
  103.                                 if($webFeatureObject -eq $null)  
  104.                                 {  
  105.                                     $resultsData | Add-Member -type NoteProperty -name $feature.Name -value "NO" -Force  
  106.                                 }  
  107.                                 else  
  108.                                 {  
  109.                                     $resultsData | Add-Member -type NoteProperty -name $feature.Name -value "YES" -Force  
  110.                                 }  
  111.                             }  
  112.                         }  
  113.                         $web.Dispose()   
  114.                     }  
  115.                     $results += $resultsData   
  116.                       
  117.                 }  
  118.                 else  
  119.                 {  
  120.                     Write-host -ForeGroundColor Red "Site Collection with this name does not exist. Please check if you have typed the URL correctly."    
  121.                 }  
  122.                 $siteCollection.Dispose()  
  123.              }  
  124.              $results | export-csv -Path $currentPhysicalPath/GetFeatureStatusResults.csv -notypeinformation -Force   
  125.              write-host "Report Exported successfully."  
  126.          }  
  127.    }  
  128. }  
  129. #EndRegion  
  130.  
  131.  
  132. #Region function call  
  133. Global_GetSiteCollectionFeatureStatus $xmlinput  
  134. #EndRegion  

Read more articles on PowerShell:


Similar Articles