Richard Dakin
Richard Dakin

Reputation: 443

PowerShell add error pages into IIS

So I am trying to write quite a simple i thought 4 each. This looks in a directory, lists a bunch of pages and then takes the name, adds it into a error page script for IIS. Will it work?! No...I get errors inconsistently from IIS. Anyone had this issue? Anyone see what i am doing wrong here?...

Directory listing is"

400.htm
401-1.htm
401-2.htm
401-3.htm
401-4.htm
401-5.htm
403-1.htm
403-10.htm
403-11.htm
403-12.htm
403-13.htm
403-14.htm
403-15.htm
403-16.htm
403-17.htm
403-2.htm
403-3.htm
403-4.htm
403-5.htm
403-6.htm
403-7.htm
403-8.htm
403-9.htm
403.htm
404-1.htm
404.htm
405.htm
406.htm
407.htm
410.htm
412.htm
414.htm
500-12.htm
500-13.htm
500-15.htm
500.htm
501.htm
502.htm
htmla.htm

My code is below:

$files = dir D:\iis\errorpages

Foreach ($file in $files)
{
    $file = $file -replace "[^0-9]"

    Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST/Website' -filter "system.webServer/httpErrors" -name "." -value @{statusCode=$file;prefixLanguageFilePath='D:\iis\errorpages';path="$file"}
} 

Upvotes: 0

Views: 581

Answers (1)

pandemic
pandemic

Reputation: 1195

$file in every loop of foreach consists an object with some properties. With your regex line $file = $file -replace "[^0-9]" I assume you want modify the file name. If I assume correctly then you should use:

$fileName = $file.name -replace "[^0-9]"

also this might be wrong:

-value @{statusCode=$file;prefixLanguageFilePath='D:\iis\errorpages';path="$file"

if you want a path and filename use property fullname

if you want a directory where it lives, you need to combine property fullname with string operations. Something like this $file.FullName.Substring(0, $file.FullName.LastIndexOf("\"))

Upvotes: 1

Related Questions