Update Host Header in IIS with Powershell

旧时模样 提交于 2019-12-01 02:58:28

问题


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?


回答1:


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}
}



回答2:


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
}


来源:https://stackoverflow.com/questions/9233366/update-host-header-in-iis-with-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!