2012-02-10 33 views
5

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

问题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?还是有更好的方法来做我想做的事情?

回答

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

不错,很好。 – Kev 2012-02-12 01:14:28

+0

不幸的是,这不适用于每个站点有多个绑定的情况。在这种情况下,$ _。ItemXPath属性值是相同的,所以只有最后一个绑定才能生效。 – esteewhy 2016-05-11 09:43:36

1

修改吉文的回答,支持多种绑定。

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 
}