Skip to content

Instantly share code, notes, and snippets.

@tiagoduarte
Created September 24, 2016 12:12
Show Gist options
  • Select an option

  • Save tiagoduarte/b77054ea2f3a36d506deffd8022e7277 to your computer and use it in GitHub Desktop.

Select an option

Save tiagoduarte/b77054ea2f3a36d506deffd8022e7277 to your computer and use it in GitHub Desktop.
enable custom errors on all web applications of a sharepoint server
#summary
#this is a server side powershell script that enables and disables debugging on a sharepoint server
#note: if you have SP2013 with webapps/wsps in 2010 mode, you'll need to run $hive on 14 folder as well
#by tiago duarte @ 31-March-2015
#http://about.me/tiagoduarte
param(
#webUrl empty will parse all web applications
$enable = $true,
$webUrl = ""
)
#usage: .\Enable-Disable-Debugging $true
#clean up the screen
clear-host
#region POWERSHELL SNIPPETS
#if the context is not administrator, stop the script and start a new shell
function Ensure-Admin($invocation)
{
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
$arguments = "& '" + $invocation.mycommand.definition + "'"
Start-Process powershell -Verb runAs -ArgumentList $arguments
throw "running an elevated window!"
}
}
#temporarily go to the folder where the script running is located
function GoTo-CurrentFolder($invocation)
{
$currentFolder = Get-Location -PSProvider FileSystem
$invokedFolder = [System.Text.RegularExpressions.Regex]::Replace($invocation.MyCommand.Definition, $invocation.MyCommand.Name, "")
if($invokedFolder -ne $currentFolder){ cd $invokedFolder }
}
#endregion
#region SHAREPOINT SERVER SNIPPETS
#add the sharepoint powershell snapin if not added already
function Add-SPSnapin()
{
If ((Get-PsSnapin |?{$_.Name -eq "Microsoft.SharePoint.PowerShell"})-eq $null)
{
Write-Host
Write-Host -ForegroundColor White " - Loading SharePoint PowerShell Snapin"
$PSSnapin = Add-PsSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue | Out-Null
Write-Host
}
}
#endregion
#region CONTEXTUAL SCRIPT SNIPPETS
function Content-Replace($path, $find, $replace)
{
$content = gc $path
if($content -match $find)
{
$content = $content -replace $find, $replace
sc -path $path $content
}
else
{
#skip already made changes, to avoid unnecessary application pool restarts
#write-host "nothing to do..."
#return $false
}
#return $true
}
function Update-Webconfig($path, $enable)
{
if(!(test-path $path))
{
throw "web.config not found in $path"
}
else
{
#perform backup
$webConfigFormat = 'web_{0:yyyy_MM_dd_hh_mm_ss}.bak' -f (Get-Date)
$destinationPath = ($path -replace "web.config", $webConfigFormat)
copy-item $path -destination $destinationPath
}
if($path -match "\\wss\\VirtualDirectories\\")
{
#parse iis config
write-host "Updating IIS web.config:"
write-host $path
write-host $nl
$trace = '<trace enabled="true" pageOutput="true" requestLimit="40" localOnly="false"/>'
if($enable)
{
Content-Replace $path 'CallStack="false"' 'CallStack="true"'
Content-Replace $path 'AllowPageLevelTrace="false"' 'AllowPageLevelTrace="true"'
Content-Replace $path 'debug="false"' 'debug="true"'
Content-Replace $path 'customErrors mode="On"' 'customErrors mode="Off"'
#add trace for all pages using custom errors
if(!((gc $path) -match "<trace"))
{
Content-Replace $path '<customErrors' ($trace + '<customErrors')
}
}
else
{
Content-Replace $path 'CallStack="true"' 'CallStack="false"'
Content-Replace $path 'AllowPageLevelTrace="true"' 'AllowPageLevelTrace="false"'
Content-Replace $path 'debug="true"' 'debug="false"'
Content-Replace $path 'customErrors mode="Off"' 'customErrors mode="On"'
#remove trace from all pages using custom errors
Content-Replace $path $trace ""
}
}
else
{
#parse hive config
write-host "Updating hive web.config:"
write-host $path
write-host $nl
if($enable)
{
Content-Replace $path 'debug="false"' 'debug="true"'
Content-Replace $path 'customErrors mode="On"' 'customErrors mode="Off"'
}
else
{
Content-Replace $path 'debug="true"' 'debug="false"'
Content-Replace $path 'customErrors mode="Off"' 'customErrors mode="On"'
}
}
}
function Setup-SharePoint-Debugging($webUrl, $enable)
{
if($enable)
{
write-warning "Enabling custom errors and debugging in $env:ComputerName"
}
else
{
write-warning "Disabling custom errors and debugging in $env:ComputerName"
}
write-host $nl
if([string]::isnullorempty($webUrl))
{
$webApps = get-spwebapplication
}
else
{
$webApps = get-spwebapplication $url
}
#find iis web.config
foreach($webApp in $webApps)
{
#write-host ("Setting server debug to " + $enable + " on " + $webApp.Url)
#$webApp = get-spwebapplication $webUrl
$iisApp = $webApp.IisSettings[[Microsoft.SharePoint.Administration.SPUrlZone]::Default]
$iisPath = $iisApp.Path.FullName
#apply customerrors and compilation
Update-Webconfig ($iisPath + "\web.config") $enable
}
#find hive web.configs
$hivePath = [Microsoft.SharePoint.Utilities.SPUtility]::GetGenericSetupPath([String]::Empty)
#apply customerrors, compilation, allowpageleveltrace, callstack, trace
Update-Webconfig ($hivePath + "\TEMPLATE\LAYOUTS\web.config") $enable
Update-Webconfig ($hivePath + "TEMPLATE\ADMIN\web.config") $enable
}
#endregion
try
{
Ensure-Admin $MyInvocation
$global:nl = [Environment]::NewLine
write-host ("Script execution started!" + $nl)
pushd
GoTo-CurrentFolder $MyInvocation
Add-SPSnapin
#work
if($webUrl -ne "")
{
$web = get-spweb $webUrl -erroraction stop
}
#call function
Setup-SharePoint-Debugging $webUrl $enable
}
catch [System.Management.Automation.ActionPreferenceStopException]
{
write-warning ("The web does not exist!")
write-host $nl
#throw $_.Exception
}
catch
{
#handle exceptions
write-warning ("Program Crashed!")
write-host ("Stack: " + $_.Exception)
write-host ("Line: " + $_.InvocationInfo.ScriptLineNumber)
write-host ("Column: " + $_.InvocationInfo.OffsetInLine)
}
finally
{
if($web -ne $null)
{
$web.Dispose()
}
popd
write-host ("Script execution ended!" + $nl)
Pause
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment