近年來設定 IIS 站台我都改用 PowerShell 指令操作(參考:再談 IIS 與 ASP.NET 網站自動安裝設定 - 使用 PowerShell),用指令檔取代 GUI 操作,能減少人為操作失誤風險,並邁向自動化部署的理想。

最近使用 Set-WebConfigurationProperty 切換匿名存取及整合式驗證遇到一個狀況:

There is a duplicate 'system.web.extensions/scripting/scriptResourceHandler' sectoin defined 是個典型問題,只需將引用 System.Web.Extensions 的 ASP.NET 3.5 網站的 AppPool Runtime 版本設成 v4.0,便能重現:

推測這是因為 PowerShell Set-WebConfigurationProperty 用 .NET 4 解析 ASP.NET 3.5 web.config 造成。Set-WebConfigurationProperty 有個 -Clr 參數 可指定 v2.0 或 4.0,但很不幸它只能用於 Machine 或 Machine/Webroot,無解。

查到替代方案 - IIS 設定傳統工具,AppCmd.exe

. $ENV:SystemRoot\System32\inetsrv\appcmd.exe set config "Default Web Site/AspNet35" /section:system.webServer/security/authentication/anonymousAuthentication /enabled:false /commit:apphost
# CMD
%SystemRoot%\System32\inetsrv\appcmd.exe set config "Default Web Site/AspNet35" /section:system.webServer/security/authentication/anonymousAuthentication /enabled:false /commit:apphost

山不轉路轉,搞定收工。

參考:IIS configuration under PowerShell runs in 4.0 CLR, causing incompatibility with < 4.0 config files

Set-WebConfigurationProperty does not work for ASP.NET 3.5 settings. Use AppCmd as an alternative.


Comments

Be the first to post a comment

Post a comment