English 中文(简体)
Constructing Active Directory entry using PowerShell works in IIS 6 but not IIS 7
原标题:

The following line of PowerShell works with IIS 6 installed:

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

However, with IIS 7 it throws the following error unless the IIS 6 Management Compatibility role service is installed:

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

My goal is to modify the HttpCustomHeaders:

$service.HttpCustomHeaders = $foo

How can I do this in an IIS-7-compliant way?

Thanks

最佳回答

There are a number of ways to do this using APPCMD and C#/VB.NET/JavaScript/VBScript:

Custom Headers (IIS.NET)

To do this using PowerShell and the Microsoft.Web.Administration assembly:

[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()

This will result in a <location> path in applicationHost.config with the following:

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

To do this in PowerShell using the new 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

This will configure a <location> path in applicationHost.config with the following:

<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>

The back-ticks at the end of each line indicate a line continuation. The two examples given above were tested on Windows 2008 Server SP2.

问题回答




相关问题
Session Management with Windows Authentication

In an ASP.NET web app, using Integrated Windows Authentication, is the session tied to the windows identity? In other words, if I login (using IWA) to the app, and the app stores some "stuff" in my ...

Using Elmah with Cassini

Does anyone know if I can use Elmah with Visual Studio build-in web server(aka Cassini)? I get it working easily on IIS, but using same configuration, it doesn t work with Cassini. When I requested ...

Setting hostname in IIS, include www?

I want to set the hostname for a website I m adding in IIS 7, however do I include the www in the hostname or not? because I want both www.mysite.com and mysite.com both to point to mysite on the ...

inetpub versus any other folder

I ve run websites out of inetpub, as well as from folders just residing on the C: drive. I wonder, are there any definitive advantages to running websites out of inetputwwwroot?

IIS 6.0 hangs when serving a web-service

I am having issues with one of our web-services. It works fine on my development machine (win XP) whether I host it as a separate application or using cassini from Visual studio. Once I deploy on the ...

热门标签