We use OctopusDeploy to create the website and we use the create website template that is on the community step sites. I have modified it slightly to ensure that if the website exists it adds the default binding however it is removing all the existing bindings. Is there a way to simply add a binding to IIS which overwrites if it already exists (or ignores) and doesn't remove all the existing bindings?
The script we currently have is as follows:
$bindingInformation = "${bindingIpAddress}:${bindingPort}:${bindingHost}"
$sitePath = ("IIS:\Sites\" + $webSiteName)
$site = Get-Item $sitePath -ErrorAction SilentlyContinue
if (!$site) {
Write-Output "Creating web site $webSiteName"
$id = (dir iis:\sites | foreach {$_.id} | sort -Descending | select -first 1) + 1
new-item $sitePath -bindings ($wsBindings[0]) -id $id -physicalPath $webRoot -confirm:$false
} else {
write-host "Web site $webSiteName already exists"
Set-ItemProperty -Path $sitePath -Name Bindings -Value ($wsBindings[0])
}
It seems this line:
Set-ItemProperty -Path $sitePath -Name Bindings -Value ($wsBindings[0])
Is overwriting all existing bindings but I can't seem to find a way around it.
You can use the New-WebBinding cmdlet:
And use Get-WebBinding cmdlet to check whether the binding already exists.