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.
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
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?
Give this a try:
Get-WebBinding -HostHeader *.stuff.* | Foreach-Object{
    #$NewHeader = $_.bindingInformation -replace '\.stuff\.','.staff.'
    Set-WebConfigurationProperty -Filter $_.ItemXPath -PSPath IIS:\ -Name Bindings -Value @{protocol='http';bindingInformation=$NewHeader}
}
                        Modified Shay's answer to support multiple bindings.
Get-WebBinding -HostHeader *.stuff.* | Foreach-Object{
    #$NewHeader = $_.bindingInformation -replace '\.stuff\.','.staff.'
    Set-WebConfigurationProperty -Filter ($_.ItemXPath + "/bindings/binding[@protocol='http' and @bindingInformation='" + $_.bindingInformation + "']") -PSPath IIS:\ -Name bindingInformation -Value $NewHeader
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With