2017-10-16 43 views
0

所以我想寫一個非常簡單的我認爲每個4。這看起來在一個目錄中,列出了一堆頁面,然後獲取名稱,並將其添加到IIS的錯誤頁面腳本中。它會起作用嗎?!不...我從IIS獲取的錯誤不一致。任何人有這個問題?任何人都看到我在做什麼錯在這裏...PowerShell將錯誤頁面添加到IIS中

目錄列表」

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 

我的代碼如下:?

$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"} 
} 
+0

*「...一個簡單的我認爲4每個」*,這是否意味着它只在'ForEach'循環中失敗,並且'Add-WebConfigurationProperty'獨立工作,插入'$ file '手動變量? – iRon

回答

1

中的foreach每次循環$file由物體與一些你的正則表達式行$file = $file -replace "[^0-9]"我假設你想修改文件名如果我假設正確那麼你應該使用:

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

也這可能是錯誤的:

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

如果你想有一個路徑和文件名使用屬性fullname

,如果你想在那裏住一個目錄,你需要財產fullname有機結合起來字符串操作。像這樣的東西$file.FullName.Substring(0, $file.FullName.LastIndexOf("\"))