Configure Web.Config Custom Errors using PowerShell


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() 
} 
}

DAYSPUG Presentation December 13th 2011


Last night I spoke to a great crowd of PowerShell Enthusiasts at the Dayton SharePoint Users Group (DAYSPUG) in Dayton, Ohio. I knew most of the folks in attendance from other local SharePoint and PowerShell events, so it was nice to see everyone again.

It was a pleasure to speak in front of all of you, and I thoroughly enjoyed the engagement that you all showed.

Since the actual presentation/demo wasn’t much different from my recent engagement at SharePoint Saturday Cincinnati, I’ll simply point to that post for the actual PowerShell and XML code I used in the demos. That content is located here: SPS Cincinnati Post

Here is a post which contains the two Christmas functions I shared last night as well: Two PowerShell Functions for the Christmas Season

Thanks again to Tony Maddin and the rest of the DAYSPUG members for welcoming me to speak. See you all next time!

RD

Two PowerShell Functions for the Christmas Season


Last night I spoke at DAYSPUG in Dayton Ohio, and since Christmas is only about 10 days away – I thought it would be fun to introduce two Christmas-related PowerShell Functions that I threw together.

The two functions are:

  1. New-ChristmasTree – This function generates an awesome, and mostly tacky animated, flashing Christmas tree in the PowerShell console.
  2. Get-DaysUntilChristmas – This function uses New-TimeSpan and Get-Date to calculate the number of days until Christmas.  The output is something like “There are 10 days until Christmas!”

Here is the New-ChristmasTree function:

function New-ChristmasTree {
[array]$Colors = "DarkRed","Green","Red","White"
$oldBG = $Host.UI.RawUI.BackgroundColor
$Host.UI.RawUI.BackgroundColor = "Black"
function New-Tree {
Clear-Host
$Color = $Colors | Get-Random
Write-Host "                         O                    " -ForegroundColor $Color
$Color = $Colors | Get-Random
Write-Host "                        OOO                    " -ForegroundColor $Color
$Color = $Colors | Get-Random
Write-Host "                       OOOOO                   " -ForegroundColor $Color
$Color = $Colors | Get-Random
Write-Host "                      OOOOOOO                    " -ForegroundColor $Color
$Color = $Colors | Get-Random
Write-Host "                     OOOOOOOOO                    " -ForegroundColor $Color
$Color = $Colors | Get-Random
Write-Host "                    OOOOOOOOOOO                    " -ForegroundColor $Color
$Color = $Colors | Get-Random
Write-Host "                   OOOOOOOOOOOOO                    " -ForegroundColor $Color
$Color = $Colors | Get-Random
Write-Host "                  OOOOOOOOOOOOOOO                    " -ForegroundColor $Color
$Color = $Colors | Get-Random
Write-Host "                 OOOOOOOOOOOOOOOOO                    " -ForegroundColor $Color
$Color = $Colors | Get-Random
Write-Host "                OOOOOOOOOOOOOOOOOOO                    " -ForegroundColor $Color
$Color = $Colors | Get-Random
Write-Host "               OOOOOOOOOOOOOOOOOOOOO                     " -ForegroundColor $Color
$Color = $Colors | Get-Random
Write-Host "             OOOOOOOOOOOOOOOOOOOOOOOOO                    " -ForegroundColor $Color
$Color = $Colors | Get-Random
Write-Host "            OOOOOOOOOOOOOOOOOOOOOOOOOOO                    " -ForegroundColor $Color
Start-Sleep -Seconds 1
}
New-Tree
New-Tree
New-Tree
New-Tree
New-Tree
New-Tree
New-Tree
New-Tree
New-Tree
New-Tree
$Host.UI.RawUI.BackgroundColor = $oldBG
Clear-Host
}

Here is the Get-DaysUntilChristmas function:

function Get-DaysUntilChristmas {
$Christmas = Get-Date 12/25
$Today = Get-Date
$TimeSpan = New-TimeSpan -Start $Today -End $Christmas
$Days = $TimeSpan.Days
Write-Host "There are $Days days until Christmas!"
}

Here is a screenshot of the Get-DaysUntilChristmas function in action: