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()
}
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?
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.