windows Powershell 更新 IIS 绑定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6383548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:29:30  来源:igfitidea点击:

Powershell update IIS Bindings

windowsiispowershellbindingiis-7

提问by ifunky

I'm trying to update the http binding on a particular site which I can do with:

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

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

The problem I'm having though is that this command completely replaces the binding information so if there is a https binding then it gets removed with Set-ItemProperty.

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

Does anyone know a way of just updating a specific binding like HTTP without having to remove the others or recreate the whole binding string?

有谁知道只更新特定绑定(如 HTTP)而不必删除其他绑定或重新创建整个绑定字符串的方法?

回答by JonnyG

The steps to UPDATE a binding in a website's binding collection, the process is actually to Get the website's binding collection, modify the specific binding, and then set the whole collection again.

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

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";

[note: Edited 20131016: Clean up answer for easier viewing ] [note: Edited 20160817: Corrected param variable type definitions ]

[注意:编辑 20131016:清理答案以便于查看] [注意:编辑 20160817:更正参数变量类型定义]

回答by northben

Here's another syntax form. I prefer this one because it seems more natural. If you don't already have the webadministration PS module loaded, import that first (import-module webadministration).

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

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

回答by Demian Martinez

Here is a Powershell script I wrote recently that can be adapted to do what you want:

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

And this is a sample output from the script above:

这是上面脚本的示例输出:

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)

Of course, the script can be adapted to do modify bindings in other ways. For example, the following script will update the IP addresses:

当然,该脚本可以适用于以其他方式修改绑定。例如,以下脚本将更新 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
        }
    }
}

回答by Subrat Biswal

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

Set-WebBinding -Name '默认网站' -BindingInformation "*:80:" -PropertyName Port -Value 12

Subrat

苏布拉特

回答by Robert