通过使用 PowerShell 编辑 Web.Config 来启用/禁用自定义错误
为了揭示 SharePoint 中的实际错误,我们过去通过编辑位于 SharePoint 虚拟目录根目录中的 web.config 文件来关闭自定义错误页面。
- 在记事本中打开 web.config
- 搜索“CallStack”,将其从“false”更改为“true”
- 搜索“CustomErrors mode”,将其值从“On”更改为“Off”
- 保存并关闭。
警告:Web.config 文件高度敏感!即使是很小的拼写错误也可能导致您的 SharePoint 网站崩溃!
典型的 SharePoint 2007“未知错误”
和 SharePoint 2010 的“发生意外错误”
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
Function ConfigureCustomError()
{
Param( [Parameter(Mandatory=$true)] [string]$WebAppURL, [parameter(Mandatory=$true)] $Option )
#Get the web application
$WebApp = Get-SPWebApplication $WebAppURL
#Get the Web Application Zone
$Zone = $WebApp.AlternateUrls[0].UrlZone
# Get the IIS settings for the zone
$IISSettings = $WebApp.IISSettings[$zone]
# Get the path from the settings
$WebConfigPath = $IISSettings.Path.ToString() + "\web.config"
#Backup web.config file
$now = [datetime]::now.ToString('dd-MM-yyyy hh-mm-ss')
$BackupFile =$IISSettings.Path.ToString()+ "\web.config.$now"
copy-item $WebConfigPath -destination $BackupFile
#Get the Web.config File
$XMLDoc = new-object System.Xml.XmlDocument
$XMLDoc.Load($WebConfigPath)
switch($option)
{
"OFF" {
#Apply Change
$XMLDoc.get_DocumentElement()."sharepoint".safemode.callstack = "True"
$XMLDoc.get_DocumentElement()."system.web".customErrors.mode = "Off"
Write-Host "Custom Error Mode has been Disabled, Now you can get the actual errors!"
}
"ON" {
$XMLDoc.get_DocumentElement()."sharepoint".safemode.callstack = "False"
$XMLDoc.get_DocumentElement()."system.web".customErrors.mode = "ON"
Write-Host "Custom Error Mode has been Enabled, SharePoint displays default custom error Page!"
}
}
$XMLDoc.Save($WebConfigPath)
}
#Call the function to Turn OFF custom errors
ConfigureCustomError "https://sharepoint.crescent.com" "OFF"


