Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update Host Header in IIS with Powershell

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?

like image 251
Chris Avatar asked Feb 10 '12 19:02

Chris


2 Answers

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}
}
like image 168
Shay Levy Avatar answered Oct 05 '22 17:10

Shay Levy


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
}
like image 31
esteewhy Avatar answered Oct 05 '22 19:10

esteewhy