Wednesday, February 13, 2013

PowerShell : Enabling ASP.NET Sessions in SharePoint

In one of my posts earlier, I explained how to enable ASP.NET sessions in SharePoint. Take a look here

I was required to achieve the same thing using PowerShell. So here is the code. This code is tested in SharePoint 2013 environment.
Function Enable-ASPSessions {
    Write-Host "Enabling ASP.NET Sessions for SharePoint Web Application..." -foregroundcolor "Cyan"

    # Get the path to the config file from $WebApp
    # Prepare variables required to extract Web.Config from SPWebApplication
    SetupEnvironment
    
    $WebAppZone = $WebApp.AlternateUrls[0].UrlZone
    $IISSettings = $WebApp.IisSettings[$WebAppZone]
    $WebAppConfigPath = $IISSettings.Path.ToString() + "\Web.Config"
    
    Write-Host "Loading Web.Config as XML in " $WebAppConfigPath -ForegroundColor Cyan
    [xml]$xml = Get-Content $WebAppConfigPath
    Write-Host "Done" -ForegroundColor Green

    # SPWebConfigModification to enable/disable CustomErrors
    
    # Check if customErrors node exists
    $customErrorsNode = $xml.SelectSingleNode("//configuration/system.web/pages")
    if ($customErrorsNode -ne $null)
    {
        Write-Host -NoNewline "pages node exists. Updating the node..." -ForegroundColor Cyan
        $configMod1 = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification 
        $configMod1.Path = "configuration/system.web/pages"
        $configMod1.Name = "enableSessionState"
        $configMod1.Sequence = 0 
        $configMod1.Owner = $SiteCollectionOwner
        $configMod1.Type = 1 
        $configMod1.Value = "true"

        # Add to the list of modifications
        $WebApp.WebConfigModifications.Add($configMod1)
        Write-Host  "Done." -ForegroundColor Green
        $WebApp.Update() 
        $WebApp.Parent.ApplyWebConfigModifications() 
    }

    # Check for system.webserver node
    $moduelsNode = $xml.SelectSingleNode("//configuration/system.webServer/modules")
    if ($moduelsNode -ne $null)
    {
        Write-Host "modules node exists in  section. Updating the node..." -ForegroundColor Cyan

        # Check for 
        $removeSessionNode = $xml.SelectSingleNode("//configuration/system.webServer/modules/remove[@name='Session']")
        if ($removeSessionNode -ne $null)
        {
            Write-Host "'' entry found in Web.Config....So no updates made for this node." -ForegroundColor Cyan
        }
        else
        {
            # Recreate the  node
            $root = $xml.get_DocumentElement();         
            $removeSessionNode = $xml.CreateNode('element',"remove","")    
            $removeSessionNode.SetAttribute("name", "Session")
            $appSettingsNode =  $xml.SelectSingleNode("//configuration/system.webServer/modules").AppendChild($removeSessionNode)

            #Apply to Web.Config
            $xml.Save($WebAppConfigPath)

            Write-Host -NoNewline "'' entry created in Web.Config." -ForegroundColor Green
        }

        # Check for 
        $addSessionNode = $xml.SelectSingleNode("//configuration/system.webServer/modules/add[@name='Session']")
        if ($addSessionNode -ne $null)
        {
            Write-Host "'' entry found in Web.Config....So no updates made for this node." -ForegroundColor Cyan
        }
        else
        {
            # Recreate the  node
            $root = $xml.get_DocumentElement();         
            $addSessionNode = $xml.CreateNode('element',"add","")    
            $addSessionNode.SetAttribute("name", "Session")
            $addSessionNode.SetAttribute("type", "System.Web.SessionState.SessionStateModule")
            $addSessionNode.SetAttribute("preCondition", "integratedMode")
            $appSettingsNode =  $xml.SelectSingleNode("//configuration/system.webServer/modules").AppendChild($addSessionNode)

            #Apply to Web.Config
            $xml.Save($WebAppConfigPath)

            Write-Host -NoNewline "'' entry created in Web.Config." -ForegroundColor Green
        }
    }

    Write-Host "ASP.NET Sessions enabled successfully." -foregroundcolor "Green"
} 

No comments:

Post a Comment