Update IIS website bindings from a CSV?

hi folks!
I need to bulk update a lot of websites in IIS10/Windows 2019 to new bindings. Each site will move to a dedicated IP.
So, what I need is to grab the current bindings (whatever they may be) and replace with an IP.
I have a CSV with websiteName, and IP address.
So, I’ll need to loop through the CSV - find the website name, then loop through the binding information and update the bindings to the IP from the CSV.

Can I assume the best way is to get the CSV imported into PS as an array, then loop through the array to update the website bindings? Or is there another more efficient way?
I found some sample code that will help with the binding information update, but I’m at a loss on how to get started here.

The issue is that each website has multiple bindings, so I’ll need to not only loop through the CSV, but when I find a website from the CSV, I’ll need to loop through all the bindings and update the IP too.
So, really multiple loops going on here :frowning:

I realise this is not a simple task, but just looking for some advice on how to get started here, so I appreciate any code snippets or ideas on how to try to solve this.

Thanks a bunch
Alex

It does not sound that hard as well. :wink:

A simple nested loop should actually do the trick. But of course it depends pretty much on the existing websites and the CSV.

This forum is for scripting questions rather than script requests. We do not write customized and ready to use scripts or solutions on request.

But your task sounds like a common task somehow. Did you try to search for a solution? I’d expect to find at least something similar. :wink:

Hi Olaf, thanks for your reply.
My apologies - I didn’t mean to ask for a custom script of course - but more so on the best approach, which is a nested loop yes. I’ll look for some more solutions via nested loop.
I have found some similar code, but it will loop through all websites on the server and replace/update the bindings for every site.

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

So I do a good starting place, but it was just some advice really I was looking for on how to handle bringing in the CSV file and then parsing that info into PS to update each website and all bindings in each website.

thanks again Olaf !

Hmmm … I understood that you have some websites and each will have multiple bindings. I’d expect the according CSV file to look something like this:

Website,IPAddresses
"Website1","IPAddress1,IPAddress2"
"Website2","IPAddress3,IPAddress4"

So you will have to loop through the websites in an outer loop and through the IP addresses in an inner loop. … so it should look something like this:

$CSVInput = @'
Website,IPAddresses
"Website1","IPAddress1,IPAddress2"
"Website2","IPAddress3,IPAddress4"
'@ |
ConvertFrom-Csv -Delimiter ','

foreach ($CSVItem in $CSVInput) {
    $IPAddressList = $CSVItem.IPAddresses -split ','
    foreach ($IPAddress in $IPAddressList) {
        [PSCustomObject]@{
            Website   = $CSVItem.Website
            IPAddress = $IPAddress
        }
    }
}

You just have to replace the PSCustomObject with the code to update your bindings.

Hi Olaf,
Thanks.

And actually - no, only 1 IP per website, but each website has multiple bindings, meaning that it has to cycle through all the binding information per website and add the IP.

for example,
website.com 80 on 192.168.1.1
website-1.com 80 on 192.168.1.1
website.com on 443 on 192.168.1
vanityalias.com on 80 on 192.168.1.1
venitalias.com on 443 on 192.168.1.1

So it would need to update all to the new IP from the CSV.

But you’re awesome, and have once again, been a star - I’ll be able to use your advice now to get something working for sure.

Cool. I’m glad it helped. :+1:t4: :smiley: