web.config file modification

01/11/2012 04:11

#web.config file modification

#1. get web.config path and access

$iisSiteName="YourSiteName"
import-module WebAdministration
$webConfigPath=(get-childitem iis:\sites |where-object {$_.Name -eq $iisSiteName}).PhysicalPath
$webConfigPath+="\web.config"
if(!(test-path $webConfigPath))
{
    write-host $webConfigPath " file doesn't exist."
    break;
}


[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Sharepoint")
[Microsoft.SharePoint.SPSecurity]::RunWithElevatedPrivileges(
 
    {
         $webConfig=[xml] (get-content -path $webConfigPath)    
        
        #2. Create Session state section in configuration/system.web/
        #<sessionState mode="InProc" cookieless="AutoDetect" timeout="20"/>
   
        $newNode=$webConfig.CreateElement("sessionState")
        #set Attributes
        $newNode.SetAttribute("mode", "InProc")
        $newNode.SetAttribute("cookieless", "AutoDetect")
        $newNode.SetAttribute("timeout", "20")
       
        $node=$webConfig.selectSingleNode("/configuration/system.web")
        $node.AppendChild($newNode)
 
                 
        #change <pages enableSessionState="false" change to 'true'
        $pagesNode=$webConfig.selectSingleNode("/configuration/system.web/pages")
        $pagesNode.SetAttribute("enableSessionState", "true");

        $webConfig.Save($webConfigPath)
        
    }
 
)