开发者

Powershell update IIS Bindings

开发者 https://www.devze.com 2023-03-13 10:56 出处:网络
I\'m trying to update the http binding on a particular site which I can do with: Set-ItemProperty \"IIS:\\Sites\\SiteName\" -Name bindings -Value @{protocol=\"http\";bindingInformation=*:80:hostname.

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

Set-ItemProperty "IIS:\Sites\SiteName" -Name bindings -Value @{protocol="http";bindingInformation=*:80:hostname.sit开发者_Python百科e.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.

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?


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 ]


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).

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


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

# 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:

# 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 'Default Web Site' -BindingInformation "*:80:" -PropertyName Port -Value 12

Subrat


Use "New-ItemProperty" to add use "Set-ItemProperty" to update an existing one.

http://www.iis.net/learn/manage/powershell/powershell-snap-in-making-simple-configuration-changes-to-web-sites-and-application-pools


I wanted to update an existing binding without wiping out the SSL Cert and other settings.

Set-WebBinding from the WebAdministration module worked for me

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

e.g.

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

For the syntax, the PropertyName denotes the piece of the binding to update and the Value is what to update it to

0

精彩评论

暂无评论...
验证码 换一张
取 消