Search code examples
bindingiis-7.5

Update site bindings in applicationHost.config


I am trying to add a list of site bindings to the applicationHost.config file as opposed to manually adding each one through IIS 7.5.

<site name="new_site" id="6">
    <application path="/" applicationPool="new_site">
        <virtualDirectory path="/" physicalPath="D:\HTTP\wwwroot\newsite" />
    </application>
    <bindings>
        <binding protocol="http" bindingInformation="*:80:example.com" />
        <binding protocol="http" bindingInformation="*:80:www.example.com" />
        <binding protocol="http" bindingInformation="*:80:example2.com" />
        <binding protocol="http" bindingInformation="*:80:www.example2.com" />
    </bindings>
</site>

The reason for doing this is i have 2000 bindings to add, which would be a lot easier to do if i could edit the config file directly.

When editing the config file however the bindings do not work or show up in IIS.

Is this actually possible or am i missing something?


Solution

  • You could script this in PowerShell:

    [Reflection.Assembly]::Load(
    "Microsoft.Web.Administration, Version=7.0.0.0, 
    Culture=Neutral, PublicKeyToken=31bf3856ad364e35") > $null
    
    $siteId = 6
    $serverManager = New-Object Microsoft.Web.Administration.ServerManager
    $site = $serverManager.Sites | where { $_.Id -eq $siteID }
    
    # Read your list of hostnames
    
    $reader = [System.IO.File]::OpenText("c:\\hostnames.txt")
    for(;;) 
    {
        $domainName = $reader.ReadLine()
        if($domainName -eq $null) { break }
        $binding1 = "*:80:www." + $domainName
        $binding2 = "*:80:" + $domainName
        $site.Bindings.Add($binding1, "http")
        $site.Bindings.Add($binding2, "http")
    }
    $serverManager.CommitChanges()
    

    The file c:\\hostnames.txt would contain a list of all your domain names, for example:

    domanname1.com
    domanname2.com
    domanname3.com
    

    If you need to bind to a specific IP address then replace * with the IP address.