Alter IIS Error Pages

We are trying to alter the IIS error pages on our website deployments, when we do and we next deploy they revert back to the defaults when we purge the installation directory and install the latest nuget package. We have tried to find a way using powershell to alter the status code paths and types but this is the best we have come up with, any help would be appreciated, we want to for example change the 404 error to execute a url on the site.

$webSiteName = $OctopusParameters['WebSiteName']
$Path = $OctopusParameters['Path']
$StatusCode = $OctopusParameters['StatusCode']

## --------------------------------------------------------------------------------------
## Helpers
## --------------------------------------------------------------------------------------
# Helper for validating input parameters
function Validate-Parameter($foo, [string[]]$validInput, $parameterName) {
    Write-Host "${parameterName}: ${foo}"
    if (! $foo) {
        throw "$parameterName cannot be empty, please specify a value"
    }
    
    if ($validInput) {
        if (! $validInput -contains $input) {
            throw "'$input' is not a valid input for '$parameterName'"
        }
    }
}

## --------------------------------------------------------------------------------------
## Validate Input
## --------------------------------------------------------------------------------------

Write-Output "Validating paramters..."
Validate-Parameter $webSiteName -parameterName "Web Site Name"
Validate-Parameter $Path -parameterName "Path"
Validate-Parameter $StatusCode -parameterName "StatusCode"


$sitePath = ("IIS:\Sites\" + $webSiteName)

Write-Output $sitePath

$site = Get-Item $sitePath -ErrorAction SilentlyContinue
if ($site) { 
    write-host "Web site $webSiteName exists"	
    
    ##Get HttpErrors array
    $HttpErrors = $site.HttpErrors
    
    ##Changing 400 error pages
    $HttpErrors | % { if ($_.HttpErrorCode -eq $StatusCode) { $_.HandlerLocation = $Path } }
    
    ##Set updated HttpErrors property
    $wmi.HttpErrors = $HttpErrors
    
    ##Save object
    $wmi.Put()
}

Hi Richard,

Thanks for getting in touch! I haven’t tried the script yet, but at first glance I noticed that the variable $wmi is not set to anything (and it doesn’t seem to be an env variable). That might be a google place to start troubleshooting the script?

Thanks

Dalmiro

Hi Dalmiro

Thanks for responding, I hadn’t spotted that and was really hopeful and changed it to:

‘’’

##Set updated HttpErrors property
$site.HttpErrors = $HttpErrors

##Save object
$site.Put()

‘’’

But still no luck.

Rich

Hi Richard,

The site object doesn’t seem to contain a member called HttpErrors. From what i’ve seen, the cmdlet Set-WebConfigurationProperty is the one that’s gonna help you achieve this.

http://forums.iis.net/t/1170588.aspx?How+use+Add+WebConfigurationProperty+for+configuring+custom+error+pages

Regards

Dalmiro

Hi Dalmiro

Thanks for the direction, I came up with this and it seems to be working well:

$webSiteName = $OctopusParameters[‘WebSiteName’]
$Path = $OctopusParameters[‘Path’]
$StatusCode = [int]$OctopusParameters[‘StatusCode’]
$ResponseMode = $OctopusParameters[‘ResponseMode’]

--------------------------------------------------------------------------------------

Helpers

--------------------------------------------------------------------------------------

Helper for validating input parameters

function Validate-Parameter($foo, [string[]]$validInput, $parameterName) {
Write-Host "${parameterName}: ${foo}"
if (! $foo) {
throw “$parameterName cannot be empty, please specify a value”
}

if ($validInput) {
    if (! $validInput -contains $input) {
        throw "'$input' is not a valid input for '$parameterName'"
    }
}

}

--------------------------------------------------------------------------------------

Validate Input

--------------------------------------------------------------------------------------

Write-Output "Validating paramters…"
Validate-Parameter $webSiteName -parameterName "Web Site Name"
Validate-Parameter $Path -parameterName "Path"
Validate-Parameter $StatusCode -parameterName “StatusCode”

$sitePath = (“IIS:\Sites” + $webSiteName)

Write-Output $sitePath

$siteError = get-WebConfiguration -Filter /System.WebServer/HttpErrors/Error[@StatusCode=$StatusCode] -PSPath $sitePath
if ($siteError) {
write-host “SiteError Exists”

Clear-WebConfiguration -Filter /System.WebServer/HttpErrors/Error[@StatusCode=$StatusCode] -PSPath $sitePath

}

add-WebConfiguration -Filter /System.WebServer/HttpErrors -Value @{StatusCode=$StatusCode; PrefixLanguageFilePath="$Null"; Path=$Path; ResponseMode=$ResponseMode} -PSPath $sitePath

Glad to hear you got it working Richard

Cheers!

This worked for me, in my case I wanted to redirect to the root of my site, for the 404 status error code.

import-module WebAdministration
Set-WebConfigurationProperty -PSPath IIS:\Sites\mdd4.react -Filter "/system.webServer/httpErrors/error[@statusCode='404']" -Name "prefixLanguageFilePath" -Value ""
Set-WebConfigurationProperty -PSPath IIS:\Sites\mdd4.react -Filter "/system.webServer/httpErrors/error[@statusCode='404']" -Name "path" -Value "/"
Set-WebConfigurationProperty -PSPath IIS:\Sites\mdd4.react -Filter "/system.webServer/httpErrors/error[@statusCode='404']" -Name "ResponseMode" -Value "ExecuteURL"