【问题标题】:Programmatically unlocking IIS configuration sections in Powershell以编程方式解锁 Powershell 中的 IIS 配置部分
【发布时间】:2011-08-08 16:44:53
【问题描述】:

我正在编写一个 powershell 脚本来创建和配置许多网站和虚拟目录。我正在使用 .NET Microsoft.Web.Administration 程序集。我在默认网站下创建了一个新应用程序,并向其中添加了一个新的虚拟目录,一切正常。我现在要做的是为虚拟目录设置身份验证选项。我在 powershell 中执行以下操作:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Administration")

$oIIS = new-object Microsoft.Web.Administration.ServerManager
$oWebSite = $oIIS.Sites["Default Web Site"]
$oApp = $oWebSite.Applications["/MyApp"]

$oConfig = $oApp.GetWebConfiguration()

$oAnonAuth = $oConfig.GetSection("system.webServer/security/authentication/anonymousAuthentication")
$oAnonAuth.SetAttributeValue("enabled", "False")

但是,SetAttributeValue 命令给了我以下错误:

"此配置部分不能在此路径中使用。当该部分在父级别锁定时会发生这种情况。锁定是默认情况下 (overrideModeDefault="Deny"),或者由具有 overrideMode=" 的位置标记显式设置拒绝”或旧的 allowOverride="false"

根据我在其他地方阅读的内容,有一些建议可以更改应用程序的 XML 文件以允许覆盖。我不想这样做 - 有没有办法以编程方式解锁配置以允许我更改它?我根本不希望任何用户输入此过程..

感谢您的帮助, 阿尔。


找到了我正在寻找的答案 - 但作为一个新用户,我无法在 24 小时内回答我自己的问题..

我想我在这个网站上找到了下面的代码,但是我的机器已经重新启动,所以我丢失了页面。但是,以下似乎有效:

#
# Allow overriding of the security settings.
#
$oGlobalConfig = $oIIS.GetApplicationHostConfiguration()
$oConfig = $oGlobalConfig.GetSection("system.webServer/security/authentication/anonymousAuthentication", "Default Web Site/mySite")
$oConfig.OverrideMode="Allow"
$oIIS.CommitChanges()

#
# Following the commit above, we need a new instance of the configuration object, which we can now 
# modify.
#
$oGlobalConfig = $oIIS.GetApplicationHostConfiguration()
$oConfig = $oGlobalConfig.GetSection("system.webServer/security/authentication/anonymousAuthentication", "Default Web Site/mySite")
$oConfig.SetAttributeValue("enabled", "False")
$oIIS.CommitChanges()

【问题讨论】:

    标签: configuration powershell iis-7.5 locked


    【解决方案1】:

    很久以前我写了一篇关于这个的博客文章。 http://www.danielrichnak.com/powershell-iis7-teach-yoursel/

    以下代码将遍历 system.webserver 级别的所有内容并将其解锁。您可以根据需要定位不同的节点。

    $assembly = [System.Reflection.Assembly]::LoadFrom("$env:systemroot\system32\inetsrv\Microsoft.Web.Administration.dll")
    
    # helper function to unlock sectiongroups
    function unlockSectionGroup($group)
    {
        foreach ($subGroup in $group.SectionGroups)
        {
            unlockSectionGroup($subGroup)
        }
        foreach ($section in $group.Sections)
        {
            $section.OverrideModeDefault = "Allow"
        }
    }
    
    # initial work
    # load ServerManager
    $mgr = new-object Microsoft.Web.Administration.ServerManager
    # load appHost config
    $conf = $mgr.GetApplicationHostConfiguration()
    
    # unlock all sections in system.webServer
    unlockSectionGroup(
         $conf.RootSectionGroup.SectionGroups["system.webServer"])
    

    您的解决方案相似但又足够不同,以至于我无法验证您所拥有的,但既然您说它有效 - 听起来不错。 :)

    【讨论】:

    • 谢谢 Daniel - 我认为它们大致相同,我只是直接跳到我感兴趣的部分。
    • 我已允许自己编辑答案并添加对 $mgr.CommitChanges() 的调用以使更改实际生效 - 如果没有该调用,代码将不会保留任何内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-25
    • 2011-10-21
    • 2015-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多