简体   繁体   中英

How do I remove IIS custom header using Powershell?

I am writing a powershell script that deploys a website to IIS 7. I would like to do the following command to remove a custom header using the Web-Administration module in powershell rather than with appcmd. How do I do this command in powershell not using appcmd?

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

To remove the header on iis level:

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

And for a specific site:

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

Adding a new custom field eg. xff-ip to have remote client ip from x-forwarded-for request header

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

Or for a specific site:

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

Removing your added custom logging field eg. xff-ip

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

Or from your site only

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

Below script is working fine for default web site. My requirement is, first I want to check the list of sites and then remove the header from IIS level as well as site level.

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

Can you please share the script.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM