Tuesday, January 29, 2013

SharePoint 2013 : PowerShell : Disabling custom errors when error occurs

This script disables customErrors in Web.Config, which allows us to see a detailed exception information rather than a generic error. This could be run either in Central Admin or SharePoint site or any other web application, strictly for debugging purposes and not production use. Some variables need to be initialized which I used SetupEnvironment function which is not here. Tested in SharePoint 2013, Windows Server 2008 environment

Function Disable-SPCustomErrors {
 Write-Host "Disabling custom errors..." -foregroundcolor "Cyan"
 # Get the path to the config file from $WebApp
 # Prepare variables required to extract Web.Config from SPWebApplication
 SetupEnvironment
 # Write-Host "Preparing required variables..." -ForegroundColor Cyan
 $WebAppZone = $WebApp.AlternateUrls[0].UrlZone
 $IISSettings = $WebApp.IisSettings[$WebAppZone]
 $WebAppConfigPath = $IISSettings.Path.ToString() + "\Web.Config"
 # Write-Host "Prepared required variables..." -ForegroundColor Green
 # 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/customErrors")
 if ($customErrorsNode -ne $null)
 { Write-Host -NoNewline "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 -NoNewline "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
 }
 # Update CallStack property in SharePoint section
 $SafeModeNode = $xml.configuration.SharePoint.SelectSingleNode("SafeMode")
 if ($SafeModeNode -ne $null)
 { Write-Host -NoNewline "SafeMode node exists. Updating the node..." -ForegroundColor Cyan
 $configMod1 = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification
$configMod1.Path = "configuration/SharePoint/SafeMode"
 $configMod1.Name = "CallStack"
 $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()
} else
 { Write-Host -NoNewline "SafeMode node does not exist. Adding the node..." -ForegroundColor Cyan
 # Node does not exist. So create it
 $root = $xml.get_DocumentElement();
$SafeModeNode = $xml.CreateNode('element',"SafeMode","")
$SafeModeNode.SetAttribute("MaxControls", "200")
 $SafeModeNode.SetAttribute("CallStack", "true")
 $SafeModeNode.SetAttribute("DirectFileDependencies", "10")
 $SafeModeNode.SetAttribute("TotalFileDependencies", "250")
 $SafeModeNode.SetAttribute("AllowPageLevelTrace", "false")
 $SystemWebNode = $xml.configuration."SharePoint".AppendChild($SafeModeNode)
 #Apply to Web.Config
 $xml.Save($WebAppConfigPath)
 Write-Host "Done." -ForegroundColor Green
 }
 Write-Host "Custom errors disabled successfully." -foregroundcolor "Green"
 }

No comments:

Post a Comment