如何通过Powershell设置IIS自定义错误?

如何通过Powershell设置IIS自定义错误?,powershell,iis,iis-8,Powershell,Iis,Iis 8,我正在尝试找出如何使用Powershell为错误401设置IIS自定义错误,因此我尝试了此方法但未成功 Set-WebConfigurationProperty -Filter "/system.webServer/httpErrors/error[@statusCode='401']" -Location IIS:\Sites\MySite -Name path -Value "~/Content/CustomErrors/401.html" 我想实现这个配置结果 提前感谢。经过长时间的错

我正在尝试找出如何使用Powershell为错误401设置IIS自定义错误,因此我尝试了此方法但未成功

Set-WebConfigurationProperty -Filter "/system.webServer/httpErrors/error[@statusCode='401']" -Location IIS:\Sites\MySite -Name path -Value "~/Content/CustomErrors/401.html"
我想实现这个配置结果


提前感谢。

经过长时间的错误循环,我终于找到了解决方法,如果有人感兴趣,我将向您展示我的解决方案

Set-Location IIS:\Sites\MySite

add-WebConfiguration -Filter /System.WebServer/HttpErrors -Value @{StatusCode=401;PrefixLanguageFilePath="$Null";  Path="~/Content/ErrorMessages/401.html"; ResponseMode="ExecuteURL"}

这对我很有用,在我的例子中,我想重定向到根目录中的index.html页面,以获取404状态错误代码

我必须:

  • 启用IIS Powershell

    import-module WebAdministration
    
  • 删除语言前缀

    Set-WebConfigurationProperty -PSPath IIS:\Sites\YOUR_SITE_NAME -Filter "/system.webServer/httpErrors/error[@statusCode='404']" -Name "prefixLanguageFilePath" -Value ""
    
  • 将路径值设置为我希望在404 http statuscode上显示的页面(在我的例子中是站点的根)

  • 更改响应模式(类型)以执行URL

    Set-WebConfigurationProperty -PSPath IIS:\Sites\YOUR_SITE_NAME -Filter "/system.webServer/httpErrors/error[@statusCode='404']" -Name "ResponseMode" -Value "ExecuteURL"
    

  • 回答得很好。另外,如果您使用的是stutus代码,如“403.4”,则需要将其拆分为“statuscode=403;substatuscode=4;”。
    Set-WebConfigurationProperty -PSPath IIS:\Sites\YOUR_SITE_NAME -Filter "/system.webServer/httpErrors/error[@statusCode='404']" -Name "ResponseMode" -Value "ExecuteURL"