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