10

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']
Steve
  • 1,557
  • 2
  • 17
  • 34

2 Answers2

24

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'}
Shay Levy
  • 121,444
  • 32
  • 184
  • 206
  • can you comment on how to add a customHeader using the Add-WebconfigurationProperty? I've been at it quite a while and finally stumbled across your answer here. – Mike Devenney Nov 18 '14 at 17:03
  • 2
    @MikeDevenney the following command will add the key value pair X-NODE, 1 to the response headers on the box Add-WebConfigurationProperty -PSPath 'MACHINE/WEBROOT/APPHOST' -Filter 'system.WebServer/httpProtocol/customHeaders' -Name . -Value @{name='X-NODE'; value='1'} – Tedford Apr 16 '15 at 21:04
  • @Tedford Thanks! So would the proper approach for adding a header to every site in IIS be to iterate over the sites collection and run the Add-WebConfigurationProperty for each one? – Mike Devenney Apr 17 '15 at 00:44
  • Shay, how do I remove other "system" headers via powershell? Q is here: http://stackoverflow.com/q/40776387/147637 – Jonesome Reinstate Monica Nov 24 '16 at 01:02
2

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'}
sahilsk
  • 133
  • 1
  • 8