Wednesday, March 21, 2012

PowerShell - Report Server Integration - Part 4

The final part of the process is to activate the Report Server Integration feature for all existing site collections.  In previous posts we configured SS2008 and SP2010.  Outside the scope of these posts, my automation process has created some base sites outside of central administration.  Because our changes apply for all new sites, the existing sites do not have access to the Report Server feature.

Microsoft tells you how to do this manually per site: http://msdn.microsoft.com/en-us/library/bb677366.aspx.

I, on the other hand, want to do this via PowerShell as a last step in auto-configuration of Report Server Integration.

First, I have a little utility function for activating features within SharePoint.

# #######################
# ##### Utility Functions #####
# #######################
# A generic function for enabling a SharePoint feature
function ActivateFeature()
{
    # Required parameters for enabling a feature
    param([string]$Identity = $(throw "-Identity parameter is required"), [string]$Url = $(throw "-Url parameter is required"))

    Enable-SPFeature -Identity $Identity -Url $Url
}
# #######################
# #######################

Second, I use that function during the process of looping through site collections on the farm.

# ############################################
# ##### Activate Report Server Integration Feature #####
# ############################################

try
{
    # Capture array of Farm SPService objects into a local variable
    $webAppSrvs = (Get-SPFarm).Services

    # Loop through each SPService object
    foreach($webService in $webAppSrvs)
    {
        Write-Log $logFile "Web Service ($($webService.TypeName)) WebApp.Cnt: $($webService.WebApplications.Count)" $lvlInfo

        # Loop through each SPWebApplication object that exists under the current SPService object.
        foreach($webApp in $webService.WebApplications)
        {
            Write-Log $logFile "Web App: $($webApp.name); Feat Cnt: $($webApp.Features.Count); Prop Cnt: $($webApp.Properties.Count)" $lvlInfo

            # Check for any SPSiteCollection objects under the SPWebApplication object
            if ($webApp.Sites.Count -gt 0)
            {
                # Loop through each SPSiteCollection object
                foreach($siteColl in $webApp.Sites)
                {
                    # Capture the current state of access denied exception handling.
                    $accessDenied = $siteColl.CatchAccessDeniedException

                    # Prevent access denied exceptions from being swallowed.
                    $siteColl.CatchAccessDeniedException = $false

                    # Activate features related to Report Server Integration
                    foreach($feature in $features)
                    {
                        try
                        {
                           ActivateFeature -Identity $feature.Identity -Url "$($siteColl.Url)"
                        }
                        catch
                        {
                            Write-Log $logFile $Error[0] $lvlWarn
                        }
                    }

                    # Return the access deniend exception handling to previous state
                    $siteColl.CatchAccessDeniedException = $accessDenied
                }
            }
        }
    }

    Write-Log $logFile "Successfully activated Report Server Integration feature on all existing site collections." $lvlInfo
}
catch
{
    Write-Log $logFile $Error[0] $lvlError
}




********************
PowerShell - Report Server Integration - Part 1
PowerShell - Report Server Integration - Part 2
PowerShell - Report Server Integration - Part 3
********************

No comments:

Post a Comment