Update Host Header in IIS with Powershell

Purpose : Update an existing host header for an IIS7.5 site using powershell

Problem : Set-WebBindingrequires a site name that I don’t have. However, I have a HostHeader.

The script . I have several sites in IIS. Some of them have a host header with a specific line 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 with .stuffin my headers.

I use Get-WebBindingto get a list of all sites and their bindings. Then I scroll through them and check if it contains bindingInformation .stuff. I modify the string as I like, and then go on to update the header with

Set-WebBinding -HostHeader $originalHeader -SetProperty HostHeader -Value $newHeader

Apparently, you should have a site name to use Set-WebBinding, unlike Get-WebBinding, which allows you to get a binding based on HostHeader ( Get-WebBinding -HostHeader $someValue). Is there a way to use Set-WebBindingwithout specifying a Namesite? Is there a way to get the site name from Get-WebBinding? Is there an alternative Set-WebBinding? Or is there a better way to do what I'm trying to do?

+5
source share
2 answers

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}
}
+4
source

Shay's modified 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
}
+1
source

All Articles