It seems like every time I go to disable custom errors for the purposes of troubleshooting, I have to turn to the interwebs to remember the exact settings I need to change and what I need to change them to. Of course it’s all done in the web.config file for the web application with which you are working – and it’s really just two settings that need tweaked…
Instead of doing this manually, I thought “What if I could create two PowerShell Functions to manipulate these values?”
Well, lucky for me I had already done this to enable and disable SP Blob Caching – so it was pretty easy to modify that code to change the customErrors and CallStack settings in the web.config.
Essentially the hardest part is making sure you grab the right configuration path from the web.config, once you’ve got that, it’s just a matter of creating a new object (Microsoft.SharePoint.Administration.SPWebConfigModification) and then setting values of that object.
Once you’ve created your 2 $configMod objects, you call the $webapp.WebConfigModifications.Add method for each mod – and then call the Update() method to apply. Hit your page again and you should see an uglier, yet more informational error message.
Once you have dot-sourced the function, here is how you disable the SharePoint (custom) Errors:
Disable-SPCustomErrors -WebApplication http://webappurl
And here is how you re-enable the SharePoint (custom) Errors:
Enable-SPCustomErrors -WebApplication http://webappurl
And without further ado, here is the code containing two functions – Enable-SPCustomErrors and Disable-SPCustomErrors:
# Disable-SPCustomErrors Function function Disable-SPCustomErrors { param( [Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)] [Microsoft.SharePoint.PowerShell.SPWebApplicationPipeBind] $WebApplication ) process { $WebApp = $WebApplication.Read() # SPWebConfigModification to enable/disable CustomErrors $configMod1 = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification $configMod1.Path = "configuration/system.web/customErrors" $configMod1.Name = "mode" $configMod1.Sequence = 0 $configMod1.Owner = "CustomErrorsMod" ## SPWebConfigModificationType.EnsureChildNode -> 0 ## SPWebConfigModificationType.EnsureAttribute -> 1 ## SPWebConfigModificationType.EnsureSection -> 2 $configMod1.Type = 1 $configMod1.Value = "Off" ###################################################################### # SPWebConfigModification to enable/disable CustomErrors $configMod2 = New-Object Microsoft.SharePoint.Administration.SPWebConfigModification $configMod2.Path = "configuration/SharePoint/SafeMode" $configMod2.Name = "CallStack" $configMod2.Sequence = 0 $configMod2.Owner = "CustomErrorsMod" ## SPWebConfigModificationType.EnsureChildNode -> 0 ## SPWebConfigModificationType.EnsureAttribute -> 1 ## SPWebConfigModificationType.EnsureSection -> 2 $configMod2.Type = 1 $configMod2.Value = "true" # Add mods, update, and apply $WebApp.WebConfigModifications.Add( $configMod1 ) $WebApp.WebConfigModifications.Add( $configMod2 ) $WebApp.Update() $WebApp.Parent.ApplyWebConfigModifications() } } # Enable-SPCustomErrors Function function Enable-SPCustomErrors { param( [Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)] [Microsoft.SharePoint.PowerShell.SPWebApplicationPipeBind] $WebApplication ) process { $WebApp = $WebApplication.Read() $mods = @() foreach ( $mod in $WebApp.WebConfigModifications ) { if ( $mod.Owner -eq "CustomErrorsMod" ) { $mods += $mod } } foreach ( $mod in $mods ) { [void] $WebApp.WebConfigModifications.Remove( $mod ) } $WebApp.Update() $WebApp.Parent.ApplyWebConfigModifications() } }
2 thoughts on “Configure Web.Config Custom Errors using PowerShell”