2009-11-10 103 views
3

PowerShell中的以下行可与IIS 6安装:构建使用PowerShell的Active Directory条目工作在IIS 6,但不是IIS 7

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

然而,IIS 7,它抛出下面的错误,除非IIS 6管理兼容性角色服务安装:

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

我的目标是修改HttpCustomHeaders:

$service.HttpCustomHeaders = $foo 

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

感谢

+0

这是预期的行为 - IIS 6管理兼容性的重点在于允许您使用IIS 6脚本管理IIS 7。 – 2009-11-11 18:44:59

+0

但是,我同意使用MWA名称空间或PS管理单元更好。通过System.DirectoryServices使用ADSI可以解决applicationHost.config文件的问题。例如,在操作脚本映射时会创建AboCustomerMapper对象。这些可能会导致长期头痛。 – Kev 2009-11-11 18:57:09

回答

3

有许多方式利用这种和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() 

这将导致<location>路径applicationHost.config下列要求:

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

要使用新的IIS 7 PowerShell Snap-In这样做在PowerShell中:

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

这将配置一个<location>路径applicationHost.config与下列:

<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上进行了测试。

相关问题