【问题标题】:Constructing Active Directory entry using PowerShell works in IIS 6 but not IIS 7使用 PowerShell 构建 Active Directory 条目适用于 IIS 6,但不适用于 IIS 7
【发布时间】:2009-11-10 22:30:41
【问题描述】:

以下 PowerShell 行适用于已安装的 IIS 6:

$service = New-Object System.DirectoryServices.DirectoryEntry("IIS://localhost/W3SVC")

但是,除非安装了 IIS 6 管理兼容性角色服务,否则使用 IIS 7 会引发以下错误:

out-lineoutput : Exception retrieving member "ClassId2e4f51ef21dd47e99d3c952918aff9cd": "Unknown error (0x80005000)"

我的目标是修改 HttpCustomHeaders:

$service.HttpCustomHeaders = $foo

如何以符合 IIS-7 的方式执行此操作?

谢谢

【问题讨论】:

  • 这是有意的行为 - IIS 6 管理兼容性的全部意义在于允许您使用 IIS 6 脚本管理 IIS 7。
  • 我同意,但是,最好使用 MWA 命名空间或 PS 管理单元。通过 System.DirectoryServices 使用 ADSI 可能会通过变通方法污染 applicationHost.config 文件。例如,在操作脚本映射时会创建 AboCustomerMapper 对象。从长远来看,这些可能会导致头痛。

标签: iis configuration iis-7 powershell adsi


【解决方案1】:

使用APPCMD 和 C#/VB.NET/JavaScript/VBScript 有多种方法:

Custom Headers (IIS.NET)

要使用 PowerShell 和 Microsoft.Web.Administration 程序集执行此操作:

[Reflection.Assembly]::Load("Microsoft.Web.Administration, Version=7.0.0.0, Culture=Neutral, PublicKeyToken=31bf3856ad364e35")

$serverManager = new-object Microsoft.Web.Administration.ServerManager

$siteConfig = $serverManager.GetApplicationHostConfiguration()
$httpProtocolSection = $siteConfig.GetSection("system.webServer/httpProtocol", "Default Web Site")
$customHeadersCollection = $httpProtocolSection.GetCollection("customHeaders")
$addElement = $customHeadersCollection.CreateElement("add")
$addElement["name"] = "X-Custom-Name"
$addElement["value"] = "MyCustomValue"
$customHeadersCollection.Add($addElement)
$serverManager.CommitChanges()

这将导致applicationHost.config 中的<location> 路径具有以下内容:

<location path="Default Web Site">
    <system.webServer>
        <httpProtocol>
            <customHeaders>
                <add name="X-Custom-Name" value="MyCustomValue" />
            </customHeaders>
        </httpProtocol>
    </system.webServer>
</location>

要在 PowerShell 中使用新的 IIS 7 PowerShell Snap-In 执行此操作:

add-webconfiguration `
   -filter /system.webServer/httpProtocol/customHeaders `
   -location "Default Web Site" `
   -pspath "IIS:" `
   -value @{name='X-MyHeader';value='MyCustomHeaderValue'} `
   -atindex 0

这将在applicationHost.config 中配置一个&lt;location&gt; 路径,内容如下:

<location path="Default Web Site">
    <system.webServer>
        <httpProtocol>
            <customHeaders>
                <clear />
                <add name="X-MyHeader" value="MyCustomHeaderValue" />
                <add name="X-Powered-By" value="ASP.NET" />
            </customHeaders>
        </httpProtocol>
    </system.webServer>
</location>

每行末尾的反引号表示续行。上面给出的两个示例是在 Windows 2008 Server SP2 上测试的。

【讨论】:

    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 2016-04-25
      • 1970-01-01
      • 1970-01-01
      • 2013-04-30
      • 1970-01-01
      • 2012-12-01
      • 2010-10-31
      • 2013-01-14
      • 2014-04-06
      相关资源
      最近更新 更多