3
votes

I'm trying to figure out how to set the IIS Custom Error for the error 401 using Powershell so I've tried this without success.

Set-WebConfigurationProperty -Filter "/system.webServer/httpErrors/error[@statusCode='401']" -Location IIS:\Sites\MySite -Name path -Value "~/Content/CustomErrors/401.html"

I want to achieve this configuration result.

Desired configuration

Thanks in advance.

2

2 Answers

2
votes

after a long try-error loop, I finally figured out how to solve this, if anyone is interested I'm going to show you my resolution.

Set-Location IIS:\Sites\MySite

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

This worked for me, in my case I wanted to redirect to the index.html page in the root, for the 404 status error code

I had to:

  1. Enable IIS Powershell.

    import-module WebAdministration
    
  2. Remove language prefix

    Set-WebConfigurationProperty -PSPath IIS:\Sites\YOUR_SITE_NAME -Filter "/system.webServer/httpErrors/error[@statusCode='404']" -Name "prefixLanguageFilePath" -Value ""
    
  3. Set the path value to the page I wanted to display on 404 http statuscode (In my case the root of the site)

    Set-WebConfigurationProperty -PSPath IIS:\Sites\YOUR_SITE_NAME -Filter "/system.webServer/httpErrors/error[@statusCode='404']" -Name "path" -Value "/"
    
  4. Change response mode (type) to Execute URL

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