Add Binding To IIS With PowerShell Without Overwriting Existing

3.7k views Asked by At

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.

2

There are 2 answers

0
Martin Brandl On BEST ANSWER

You can use the New-WebBinding cmdlet:

  New-WebBinding `
        -Name $webSiteName `
        -Protocol 'http' `
        -Port $bindingPort `
        -IPAddress $bindingIpAddress `
        -HostHeader $bindingHost

And use Get-WebBinding cmdlet to check whether the binding already exists.

0
Erti-Chris Eelmaa On

You can use New-WebBinding as jisaak said, that's mainly for http(s).

If you need to add other types of bindings, you have to use New-ItemProperty, instead of Set-ItemProperty.

New-ItemProperty -path "IIS:\Sites\YourSiteName" `
    -name bindings -value @{protocol="net.pipe";bindingInformation="SiteName"}