Powershell update IIS Bindings

Viewed 29332

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

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?

6 Answers

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
        }
    }
}

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://docs.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

Related