Powershell update IIS Bindings

28,946

Solution 1

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 ]

Solution 2

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

Solution 3

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

Solution 4

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

Subrat

Solution 5

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

Share:
28,946

Related videos on Youtube

ifunky
Author by

ifunky

.Net contractor based in London, I like all things CC.NET and agile! Bespoke website development by iFunky.Net

Updated on July 09, 2022

Comments

  • ifunky
    ifunky almost 2 years

    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?

  • Tomas Jansson
    Tomas Jansson almost 11 years
    I don't think this solution is good enough since it will delete all bindings if you have more than one and just leave you with the one specified.
  • Tomas Jansson
    Tomas Jansson almost 11 years
    I don't agree that it exactly answers the question, but I think this is as good as it gets with powershell against IIS. What I would like to see is something like $b = Get-WebBinding; $b.SetAttribute('Port', 9999); $b.Save; but that doesn't work. I can't remove the downvote unless you change the answer (!)... weird.
  • JonnyG
    JonnyG almost 11 years
    Spoonfed solution has been provided in the answer via change. Please review your downvote.
  • kddeisz
    kddeisz over 10 years
    You should try to include a little bit more explanation in your answer.
  • northben
    northben over 9 years
    yes - here is some more info on how to do that: iis.net/learn/manage/powershell/…
  • JamesQMurphy
    JamesQMurphy about 9 years
    Note that this commandlet was introduced with Windows Server 2012. If you're still on Server 2008, you'll have to go with JohnnyG's solution.
  • JonnyG
    JonnyG about 8 years
    Worthy of note: New-WebBinding cmdlet does not actually UPDATE a specific binding (replace an old binding value with a new binding value) - as specified in the original question; but does add a New binding to the binding list on the site. If you need to Update an existing binding, you'll have to use my function above.
  • wexman
    wexman almost 7 years
    Agreed, as long as you don't need msmq binding which get's screwed when added this way
  • Shafiq Jetha
    Shafiq Jetha over 5 years
    To clarify this a little for everyone else, the first two parameters, that is the name and the BindingInformation, are actually search criteria for the change. The 3rd and 4th parameters are the things that need to be changed. In the above example we are searching for the Default Web Site, and we want to adjust the port 80 binding to be port 12 instead. This ended up being what worked for me so I thought I'd add to the description to help people out in the future.
  • rahulaga-msft
    rahulaga-msft over 5 years
    @JonnyG I am facing issue exactly similar to one in OP. The solution you provided is workaround for some sort of bug ? Or it is expected that existing bindings will be wiped off when using cmdlt Set-ItemProperty while updating http binding ?