IIS 6ではなく、PowerShellを使用してActive Directoryエントリの構築
-
19-09-2019 - |
質問
PowerShellの次のラインは、IIS 6インストールされています。
$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準拠の方法でこれを行うにはどうすればよいですか?
ありがとう
解決
これを使用する方法はいくつかあります APPCMD
およびc#/vb.net/javascript/vbscript:
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でこれを行うには PowerShellスナップイン:
add-webconfiguration `
-filter /system.webServer/httpProtocol/customHeaders `
-location "Default Web Site" `
-pspath "IIS:" `
-value @{name='X-MyHeader';value='MyCustomHeaderValue'} `
-atindex 0
これはaを構成します <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>
各ラインの終わりにあるバックチックは、行の継続を示しています。上記の2つの例は、Windows 2008サーバーSP2でテストされました。
他のヒント
現在、IIS 7 PowerShellスナップインがあります。
http://learn.iis.net/page.aspx/428/getting-with-the-iis-70-powershell-snap-in/
所属していません StackOverflow