使用Powershell更新IIS中的主机标头

时间:2012-02-10 19:08:30

标签: powershell iis-7.5

目标:使用powershell更新IIS7.5站点的现有主机标头

问题Set-WebBinding需要我没有的网站名称。我确实有HostHeader。

场景:我在IIS中有多个站点。其中一些有一个主机头,其中包含我想要更改的特定字符串。

Site1 - site1.stuff.domain.net
Site2 - site2.stuff.domain.net
Site3 - site3.domain.net

我想更改标题中包含.stuff的所有网站。

我正在使用Get-WebBinding来获取所有网站及其绑定的列表。然后我循环遍历它们并检查bindingInformation是否包含.stuff。我按字母方式修改字符串,然后用

更新标题
Set-WebBinding -HostHeader $originalHeader -SetProperty HostHeader -Value $newHeader

显然,您必须拥有该网站的名称才能使用Set-WebBinding,这与Get-WebBinding不同,后者允许您根据HostHeader(Get-WebBinding -HostHeader $someValue)获取绑定。有没有办法在没有指定网站的Set-WebBinding的情况下使用Name?有没有办法从Get-WebBinding获取网站的名称?有Set-WebBinding的替代方案吗?或者有更好的方法来做我想做的事情吗?

2 个答案:

答案 0 :(得分:4)

尝试一下:

Get-WebBinding -HostHeader *.stuff.* | Foreach-Object{
    #$NewHeader = $_.bindingInformation -replace '\.stuff\.','.staff.'
    Set-WebConfigurationProperty -Filter $_.ItemXPath -PSPath IIS:\ -Name Bindings -Value @{protocol='http';bindingInformation=$NewHeader}
}

答案 1 :(得分:1)

修改Shay的答案以支持多个绑定。

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
}
相关问题