如何使用Powershell删除IIS自定义标头?

时间:2013-08-09 17:01:23

标签: iis powershell iis-7

我正在编写一个将网站部署到IIS 7的PowerShell脚本。我想使用PowerShell中的Web管理模块而不是appcmd执行以下命令来删除自定义标头。如何在PowerShell中执行此命令而不使用appcmd?

appcmd set config /section:httpProtocol /-customHeaders.[name='X-Powered-By']

2 个答案:

答案 0 :(得分:19)

删除iis级别的标题:

Remove-WebConfigurationProperty -PSPath MACHINE/WEBROOT/APPHOST  
                                -Filter system.webServer/httpProtocol/customHeaders 
                                -Name . 
                                -AtElement @{name='X-Powered-By'}

对于特定网站:

Remove-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST/Default Web Site'
                                -Filter system.webServer/httpProtocol/customHeaders
                                -Name .
                                -AtElement @{name='X-Powered-By'}

答案 1 :(得分:0)

添加新的自定义字段,例如。 xff-ipx-forwarded-for请求标头

获取远程客户端IP
Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST'  -filter "system.applicationHost/sites/siteDefaults/logFile/customFields" -name "." -value @{logFieldName='xff-ip';sourceName='X-FORWARDED-FOR';sourceType='RequestHeader'}

或针对特定网站:

Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -filter "system.applicationHost/sites/site[@name='My Super Site']/logFile/customFields"  -name "." -value @{logFieldName='xff-ip';sourceName='X-FORWARDED-FOR';sourceType='RequestHeader'}

删除添加的自定义日志记录字段,例如xff-ip

Remove-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -filter "system.applicationHost/sites/siteDefaults/logFile/customFields" -name "."  -AtElement @{logFieldName='xff-ip'}

或仅从您的网站

Remove-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -filter "system.applicationHost/sites/site[@name='My Super Site']/logFile/customFields"  -name "." -AtElement @{logFieldName='xff-ip'}