【问题标题】:Powershell update IIS BindingsPowershell 更新 IIS 绑定
【发布时间】:2011-06-17 09:01:30
【问题描述】:

我正在尝试更新我可以使用的特定站点上的 http 绑定:

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

我遇到的问题是这个命令完全替换了绑定信息,所以如果有一个 https 绑定,那么它会被 Set-ItemProperty 删除。

有没有人知道一种只更新特定绑定(如 HTTP)而无需删除其他绑定或重新创建整个绑定字符串的方法?

【问题讨论】:

    标签: windows iis powershell binding iis-7


    【解决方案1】:

    在一个网站的绑定集合中UPDATE一个绑定的步骤,这个过程其实就是获取网站的绑定集合,修改具体的绑定,然后重新设置整个集合。

    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:更正了参数变量类型定义]

    【讨论】:

    • 我认为这个解决方案不够好,因为如果您有多个绑定,它会删除所有绑定,只留下指定的一个。
    • 我不同意它完全回答了这个问题,但我认为这与针对 IIS 的 powershell 一样好。我想看到类似$b = Get-WebBinding; $b.SetAttribute('Port', 9999); $b.Save; 的东西,但这不起作用。除非您更改答案(!),否则我无法删除反对票...很奇怪。
    • 已通过更改在答案中提供了 Spoonfed 解决方案。请查看您的反对意见。
    • @JonnyG 我面临的问题与 OP 中的问题完全相同。您提供的解决方案是某种错误的解决方法?或者预计在更新http 绑定时使用cmdlt Set-ItemProperty 时现有绑定将被清除?
    【解决方案2】:

    这是另一种语法形式。我更喜欢这个,因为它看起来更自然。如果您尚未加载 webadministration PS 模块,请先导入该模块 (import-module webadministration)。

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

    【讨论】:

    • 是的 - 这里有一些关于如何做到这一点的更多信息:iis.net/learn/manage/powershell/…
    • 请注意,此命令行开关是随 Windows Server 2012 引入的。如果您仍在使用 Server 2008,则必须使用 JohnnyG 的解决方案。
    • 值得注意的是:New-WebBinding cmdlet 实际上并没有更新特定的绑定(用新的绑定值替换旧的绑定值) - 如原始问题中所述;但确实将新绑定添加到站点上的绑定列表中。如果您需要更新现有绑定,则必须使用我上面的函数。
    • 同意,只要您不需要 msmq 绑定,这样添加时会搞砸
    【解决方案3】:

    这是我最近编写的一个 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
            }
        }
    }
    

    【讨论】:

      【解决方案4】:

      Set-WebBinding -Name 'Default Web Site' -BindingInformation "*:80:" -PropertyName Port -Value 12

      子组

      【讨论】:

      • 你应该尝试在你的答案中包含更多的解释。
      • 为了让其他人稍微澄清一下,前两个参数,即名称和 BindingInformation,实际上是更改的搜索条件。第三和第四个参数是需要改变的东西。在上面的示例中,我们正在搜索默认网站,并且我们希望将端口 80 绑定调整为端口 12。这最终对我有用,所以我想我会添加到描述中以帮助未来的人们。
      【解决方案5】:

      【讨论】:

        【解决方案6】:

        我想在不清除 SSL 证书和其他设置的情况下更新现有绑定。

        WebAdministration 模块中的 Set-WebBinding 对我有用

        https://docs.microsoft.com/en-us/powershell/module/webadminstration/set-webbinding?view=winserver2012-ps

        例如

        Set-WebBinding -PropertyName HostHeader -Value newhostnamevalue.site.net -HostHeader hostname.site.net -IPAddress * -Name MyWebSite -Port 80
        

        对于语法,PropertyName 表示要更新的绑定部分,而 Value 是要更新的内容

        【讨论】:

          猜你喜欢
          • 2021-03-29
          • 2018-07-09
          • 2015-11-12
          • 2023-03-13
          • 1970-01-01
          • 2012-04-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多