Wednesday, January 30, 2013

SharePoint 2013 : PowerShell : Disable custom errors in LAYOUTS folder

If you want to see detailed exception information when an error occurs during Application Page process, we need to turn off custom errors within in that folder. Check my earlier post on this here. To disable custom errors using PowerShell, here is the script (required variables need to be initialized in SetupEnvironment function which is not included here)


 






Function Disable-SPCustomErrors-LAYOUTS-Folder {

 # Get the path to the config file from $WebApp

 

# Prepare variables required to extract Web.Config from SPWebApplication

 SetupEnvironment

 $LAYOUTSConfigPath = $SharePointLAYOUTSFolder + "Web.Config"

 Write-Host -NoNewline "Loading Web.Config as XML in " $LAYOUTSConfigPath -ForegroundColor Cyan

 [xml]$xml = Get-Content $LAYOUTSConfigPath 

Write-Host "Done" -ForegroundColor Green

 # SPWebConfigModification to enable/disable CustomErrors

 # Check if customErrors node exists


$systemwebNode = $xml.SelectSingleNode("//configuration/system.web/customErrors")

 if ($systemwebNode -ne $null)

 { Write-Host "cusomErrors node exists. Updating the node..." -ForegroundColor Cyan

 $configMod1 = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification 

$configMod1.Path = "configuration/system.web/customErrors"

 $configMod1.Name = "mode"

 $configMod1.Sequence = 0 

$configMod1.Owner = $SiteCollectionOwner

 $configMod1.Type = 1 

$configMod1.Value = "Off"

 

# Add to the list of modifications

 $WebApp.WebConfigModifications.Add($configMod1)

 Write-Host "Done." -ForegroundColor Green

 $WebApp.Update() 

$WebApp.Parent.ApplyWebConfigModifications() 

}
else

 {
         Write-Host "cusomErrors node does not exist. Adding the node..." -ForegroundColor Cyan


 # Node does not exist. So create it
 
 $root = $xml.get_DocumentElement(); 

        

$customErrorsNode = $xml.CreateNode('element',"customErrors","") 

$customErrorsNode.SetAttribute("mode", "Off")
 
 $SystemWebNode = $xml.configuration."system.web".AppendChild($customErrorsNode)
 
 #Apply to Web.Config
 
 $xml.Save($WebAppConfigPath)
 
 Write-Host "Done." -ForegroundColor Green
  }
  } 

No comments:

Post a Comment