2011-06-17 54 views
14

我試圖更新HTTP特定網站上的結合,我可以用做:PowerShell的更新IIS綁定

Set-ItemProperty "IIS:\Sites\SiteName" -Name bindings -Value @{protocol="http";bindingInformation=*:80:hostname.site.net} 

我有但問題是,這個命令完全取代綁定信息,以便如果有一個https綁定,那麼它將被Set-ItemProperty刪除。

有沒有人知道只更新像HTTP一樣的特定綁定的方式,而不必刪除其他人或重新創建整個綁定字符串?

回答

17

更新網站綁定集合中綁定的步驟,該過程實際上是獲取網站的綁定集合,修改特定的綁定,然後再次設置整個集合。

Function ReplaceWebsiteBinding { 

    Param(
     [string] $sitename, 
     [string] $oldBinding, 
     [string] $newValue 
    ); 
    import-module webadministration; 
    $wsbindings = (Get-ItemProperty -Path "IIS:\Sites\$sitename" -Name Bindings) 
    for($i=0;$i -lt ($wsbindings.Collection).length;$i++){ 
     if((($wsbindings.Collection[$i]).bindingInformation).Contains($oldBinding)){ 
      ($wsbindings.Collection[$i]).bindingInformation = $newValue; 
     } 
    } 
    Set-ItemProperty -Path "IIS:\Sites\$sitename" -Name Bindings -Value $wsbindings 
} 

ReplaceWebsiteBinding "Default Web Site" "*:80:" "192.168.1.101:80:SomeHostHeader.domain"; 

[注:編輯20131016:清理答案以方便查看] [注:編輯20160817:修正PARAM變量類型定義]

+0

我不認爲這個解決方案是不夠好,因爲它會刪除所有綁定,如果你有一個以上的,只是留給你指定的一個。 – 2013-05-12 11:39:45

+0

我不同意它完全回答這個問題,但我認爲這與使用針對IIS的powershell一樣好。我想看到的是'$ b = Get-WebBinding; $ b.SetAttribute('Port',9999); $ b.Save;'但那不行。我不能刪除downvote,除非你改變答案(!)......怪異。 – 2013-05-13 13:21:55

+0

通過改變提供了勺子解決方案。請檢查你的downvote。 – JonnyG 2013-05-28 12:36:28

15

這裏的另一種語法形式。我更喜歡這個,因爲它看起來更自然。如果您尚未加載Web管理PS模塊,請首先導入(import-module webadministration)。

New-WebBinding -name test03 -port 443 -Protocol https -HostHeader test03.int -IPAddress "*" 
+1

您可以使用此命令添加SSL證書嗎? – 2015-01-26 09:14:22

+1

是的 - 這裏有一些關於如何做到這一點的更多信息:http://www.iis.net/learn/manage/powershell/powershell-snap-in-configuring-ssl-with-the-iis-powershell-snap-在 – northben 2015-01-26 23:29:35

+0

請注意,這個commandlet是隨Windows Server 2012一起推出的。如果你仍然在Server 2008上,你將不得不使用JohnnyG的解決方案。 – JamesQMurphy 2015-02-11 15:48:30

1

設置-WebBinding -Name '默認Web站點' -BindingInformation 「*:80:」 -propertyname港 - 數值12

Subrat

+0

你應該嘗試在你的答案中包含更多的解釋。 – kddeisz 2014-01-08 20:27:51

1

這裏是一個PowerShell腳本我最近寫的是可適應你想要做什麼:

# Updates IIS bindings across all sites by replacing all occurrences 
# of $searchString for $replaceString in the binding host header. 
# Note that the search and replace is case insensitive. 

$searchString = "ovh-ws0" 
$replaceString = "ovh-ws1" 
foreach ($website in Get-Website) { 
    "Site: {0}" -f $website.name 
    $bindings = Get-WebBinding -Name $website.name 
    foreach ($binding in $website.bindings.Collection) { 
     $bindingInfo = $binding.bindingInformation 
     " Binding: {0}" -f $bindingInfo 
     if ($bindingInfo -imatch $searchString) { 
      $oldhost = $bindingInfo.Split(':')[-1] 
      $newhost = $oldhost -ireplace $searchString, $replaceString 
      "  Updating host: {0} ---> {1}" -f $oldhost, $newhost 
      Set-WebBinding -Name $website.name -BindingInformation $bindingInfo -PropertyName "HostHeader" -Value $newhost 
     } 
    } 
} 

這是從上面的腳本輸出樣本:

Site: alpha 
    Binding: 100.101.102.103:80:alpha.redacted.com 
    Binding: 100.101.102.103:80:ovh-ws0-alpha.redacted.com 
     Updating host: ovh-ws0-alpha.redacted.com ---> ovh-ws1-alpha.redacted.com 
    Binding: 100.101.102.103:443:ovh-ws0-alpha.redacted.com 
     Updating host: ovh-ws0-alpha.redacted.com ---> ovh-ws1-alpha.redacted.com 
    Binding: 100.101.102.103:443:alpha.redacted.com 
Site: beta 
    (etc) 
Site: release 
    (etc) 

當然,腳本可以適應於以其他方式修改綁定。例如,下面的腳本將更新IP地址:

# Updates IIS bindings across all sites by replacing all IP addresses from $oldIP to $newIP. 

$oldIP = "100.101.102.103" 
$newIP = "*" 
foreach ($website in Get-Website) { 
    "Site: {0}" -f $website.name 
    $bindings = Get-WebBinding -Name $website.name 
    foreach ($binding in $website.bindings.Collection) { 
     $bindingInfo = $binding.bindingInformation 
     " Binding: {0}" -f $bindingInfo 
     if ($bindingInfo -imatch $oldIP) { 
      "  Updating IP: {0} ---> {1}" -f $oldIP, $newIP 
      Set-WebBinding -Name $website.name -BindingInformation $bindingInfo -PropertyName "IPAddress" -Value $newIP 
     } 
    } 
}