Change all bindings in IIS to new IP

So I was migrating a web server from IIS 6 to IIS 8.5.
After the migration i needed to change IP on the new server. That’s not fun when all bindings are with IP.

So I decided to just Google a script that could change everything for me. I only found scripts that could change for a specific website and not all of them. So after some copy past and some trial and error I got this is result:

Import-Module Webadministration

$OldIP = "192.168.2.68:80"
$NewIP = "*:80"

$SiteNames = Get-ChildItem -Path IIS:\Sites | Select-Object name #-First 1

foreach ($name in $SiteNames){
    $txtName = $name.name
    #$name.name.gettype()
    
    $wsbindings = (Get-ItemProperty -Path "IIS:\Sites\$txtName" -Name Bindings)
    for($i=0;$i -lt ($wsbindings.Collection).length;$i++){
        $tmp = $wsbindings.Collection[$i].bindingInformation
        if ($tmp -match $OldIP){
            $tmp = $tmp -replace $OldIP, $NewIP 
            ($wsbindings.Collection[$i]).bindingInformation = $tmp;
            Set-ItemProperty -Path "IIS:\Sites\$txtName" -Name Bindings -Value $wsbindings
        }
    }
}

 

If you find it useful please leave a comment.

/Fredrik

Leave a Reply

Your email address will not be published.