PowerShell Script To Find Where SharePoint 2007 Features Are Activated

   Recently I posted a script to find where SharePoint 2010 Features Are Activated.  I built the original version to use SharePoint 2010 PowerShell commandlets as that saved me a number of steps for filtering and gathering features at each level.  If there was ever demand for a 2007 version I could modify the script to handle that by using the object model instead of commandlets.  Just the other week a fellow SharePoint PFE Jason Gallicchio had a customer asking about a version for SharePoint 2007.  With a little bit of work I was able to convert the script to work against SharePoint 2007.

 

Solution

   Below is the converted script that works against a SharePoint 2007 farm.  Note: There appears to be a bug with the 2007 version that does not give accurate results against a SharePoint 2010 farm.  I ran the 2007 version against a 2010 farm and got fewer results than my 2010 version of the script.  Discussing with some fellow PFEs I think the discrepancy may be due to sandboxed features, a new concept in SharePoint 2010.  I have not had enough time to test or confirm.  For the time being only use the 2007 version script against SharePoint 2007 farms and the 2010 version against SharePoint 2010 farms.

   Note: This script is not optimized for medium to large farms.  In my testing it took 1-3 minutes to recurse through my demo environment.  This script is provided as-is with no warranty.  Run this in a smaller dev / test environment first.

function Get-SPFeatureActivated 
{ 
# see full script for help info, removed for formatting 
[CmdletBinding()] 
param( 
    [Parameter(position = 1, valueFromPipeline=$true)] 
    [string] 
    $Identity 
)#end param 
    Begin 
    { 
        # load SharePoint assembly to access object model 
        [void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”) 
    
        # declare empty array to hold results. Will add custom member for Url to show where activated at on objects returned from Get-SPFeature. 
        $results = @() 
        
        $params = @{} 
    } 
    Process 
    { 
        if([string]::IsNullOrEmpty($Identity) -eq $false) 
        { 
            $params = @{Identity = $Identity} 
        } 
        
        # create hashtable of farm features to lookup definition ids later 
        $farm = [Microsoft.SharePoint.Administration.SPFarm]::Local 
                
        # check farm features 
        $results += ($farm.FeatureDefinitions | Where-Object {$_.Scope -eq “Farm”} | Where-Object {[string]::IsNullOrEmpty($Identity) -or ($_.DisplayName -eq $Identity)} | 
                         % {Add-Member -InputObject $_ -MemberType noteproperty -Name Url -Value ([string]::Empty) -PassThru} | 
                         Select-Object -Property Scope, DisplayName, Id, Url) 
        
        # check web application features 
        $contentWebAppServices = $farm.services | ? {$_.typename -like “Windows SharePoint Services Web Application”} 
        
        foreach($webApp in $contentWebAppServices.WebApplications) 
        { 
            $results += ($webApp.Features | Select-Object -ExpandProperty Definition | Where-Object {[string]::IsNullOrEmpty($Identity) -or ($_.DisplayName -eq $Identity)} | 
                         % {Add-Member -InputObject $_ -MemberType noteproperty -Name Url -Value $webApp.GetResponseUri(0).AbsoluteUri -PassThru} | 
                         Select-Object -Property Scope, DisplayName, Id, Url) 
            
            # check site collection features in current web app 
            foreach($site in ($webApp.Sites)) 
            { 
                $results += ($site.Features | Select-Object -ExpandProperty Definition | Where-Object {[string]::IsNullOrEmpty($Identity) -or ($_.DisplayName -eq $Identity)} | 
                                 % {Add-Member -InputObject $_ -MemberType noteproperty -Name Url -Value $site.Url -PassThru} | 
                                 Select-Object -Property Scope, DisplayName, Id, Url) 
                
                # check site features in current site collection 
                foreach($web in ($site.AllWebs)) 
                { 
                    $results += ($web.Features | Select-Object -ExpandProperty Definition | Where-Object {[string]::IsNullOrEmpty($Identity) -or ($_.DisplayName -eq $Identity)} | 
                                     % {Add-Member -InputObject $_ -MemberType noteproperty -Name Url -Value $web.Url -PassThru} | 
                                     Select-Object -Property Scope, DisplayName, Id, Url)                    
                
                    $web.Dispose() 
                } 
                $site.Dispose() 
            } 
        } 
    } 
    End 
    { 
        $results 
    } 
} #end Get-SPFeatureActivated 

Get-SPFeatureActivated 

Conclusion

   I have posted this script to the TechNet Script Repository (click here).  As always I appreciate any feedback on scripts.  If anyone is motivated to run this 2007 version script against a SharePoint 2010 to see if they find any differences in number of features reported versus what they get with the 2010 version script I’d love to hear from you.

 

      -Frog Out

2 thoughts on “PowerShell Script To Find Where SharePoint 2007 Features Are Activated

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s