Question:
Goal: Update an existing host header for an IIS7.5 site with powershell
Problem: Set-WebBinding
requires the name of the site which I don’t have. I do have the HostHeader though.
Scenario: I have multiple sites in IIS. Some of them have a host header with a particular string that I want to change.
1 2 3 4 |
Site1 - site1.stuff.domain.net Site2 - site2.stuff.domain.net Site3 - site3.domain.net |
I want to change all sites that have .stuff
in their headers.
I’m using Get-WebBinding
to get a list of all sites and their bindings. I then loop through them and check if bindingInformation
contains .stuff
. I modify the string how I please and then go to update the header with
1 2 |
Set-WebBinding -HostHeader $originalHeader -SetProperty HostHeader -Value $newHeader |
Apparently though, you have to have the site’s name in order to use Set-WebBinding
, unlike Get-WebBinding
which allows you to get a binding based on the HostHeader (Get-WebBinding -HostHeader $someValue
). Is there a way to use Set-WebBinding
without specifing the Name
of a site? Is there a way I can get the site’s name from Get-WebBinding
? Is there an alternative to Set-WebBinding
? Or is there a better way to do what I’m trying to do?
Answer:
Give this a try:
1 2 3 4 5 |
Get-WebBinding -HostHeader *.stuff.* | Foreach-Object{ #$NewHeader = $_.bindingInformation -replace '\.stuff\.','.staff.' Set-WebConfigurationProperty -Filter $_.ItemXPath -PSPath IIS:\ -Name Bindings -Value @{protocol='http';bindingInformation=$NewHeader} } |