N.S
N.S

Reputation: 91

adding custom headers using powershell script

I am trying to write a powershell script that i can use to add custom http headers in IIS instead of manually adding them. I did not have any luck finding anything on the web so far. Can someone please point me to right direction.

Thanks

Upvotes: 3

Views: 8498

Answers (2)

N.S
N.S

Reputation: 91

I was able to successfully add 2 or more headers using the following:

Add-WebConfigurationProperty -PSPath MACHINE/WEBROOT/APPHOST `
    -Name . -Filter system.webServer/httpProtocol/customHeaders `
    -AtElement @{name = "X-Frames-Options" ; value = 'sameorigin' }

Add-WebConfigurationProperty -PSPath MACHINE/WEBROOT/APPHOST `
    -Name . -Filter system.webServer/httpProtocol/customHeaders `
    -AtElement @{name = "Cache-Control" ; value = 'private, no-cache, no-store, max-age=0'}

Upvotes: 6

Saurabh R S
Saurabh R S

Reputation: 3177

As an alternative, avoiding the cmdlets and going direct to appcmd still works:

PS> C:\Windows\System32\inetsrv\appcmd.exe set config  -section:system.webserver/httpProtocol /+"customHeaders.[name='myCustomName',value='myCustomValue']"

Upvotes: 2

Related Questions