以编程方式解锁Powershell中的IIS配置部分

Al *_*son 10 powershell configuration locked iis-7.5

我正在编写一个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")
Run Code Online (Sandbox Code Playgroud)

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

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

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

谢谢你的帮助,Al.


找到了我想要的答案 - 但作为新用户我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()
Run Code Online (Sandbox Code Playgroud)

Dan*_*nak 7

我写了一篇关于这篇文章的博文.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"])
Run Code Online (Sandbox Code Playgroud)

你的解决方案很相似,但又不同,我无法验证你得到了什么,但是既然你说它有效 - 听起来不错.:)

  • 我允许自己编辑答案并添加对`$ mgr.CommitChanges()`的调用以使更改实际生效 - 如果没有该调用,代码不会保留任何内容. (4认同)