下面这行PowerShell可以在安装了IIS6的情况下运行:
$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的方式执行此操作?
谢谢
发布于 2009-11-12 01:59:21
使用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>要使用新的IIS7 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这将使用以下内容在applicationHost.config中配置<location>路径:
<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>每行末尾的反勾号表示行的延续。上面给出的两个例子是在Windows2008Server SP2上测试的。
发布于 2009-11-11 06:35:41
现在有一个IIS7 PowerShell管理单元:
http://learn.iis.net/page.aspx/428/getting-started-with-the-iis-70-powershell-snap-in/
https://stackoverflow.com/questions/1711706
复制相似问题