繁体   English   中英

Powershell 更新 IIS 绑定

[英]Powershell update IIS Bindings

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

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

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

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

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

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

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

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

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

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

苏布拉特

我想在不清除 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 是将其更新为的内容

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM