powershelliis-10

Remove-WebBindings removing binding from all IIS sites


I am creating a Powershell script using the WebAdministration module to create a couple of sites and automatically add bindings

Import-Module WebAdministration

$config = @{
    Sites = @( 
        @{
            Name = "Site1";
            Path = "Path1";
            Bindings = @(
                @{ Protocol = "http"; Port = 80;},
                @{ Protocol = "https"; Port = 443;}
            );
        },
        @{
            Name = "Site2";
            Path = "Path2";
            Bindings = @(
                @{ Protocol = "http"; Port = 3009;}
            );
        }
    )
}

foreach($site in $config.Sites){
    $physicalPath = Get-Item "$($site.Path)"
    # Create the current site
    New-WebSite -Name "$($site.Name)" -PhysicalPath $physicalPath.FullName

    ## Trying to remove default port 80 binding for the current site
    Remove-WebBinding -Name $site.Name -Port 80 -Protocol "http"

    ## Add the desired bindings
    foreach ($binding in $site.Bindings){
        New-WebBinding -Name "$($site.Name)" -Protocol $binding.Protocol -Port $binding.Port
    }
}

When I do this though, I'm left without a binding on port 80 Site1. It looks like Remove-WebBinding -Name $site.Name -Port 80 -Protocol "http" is removing bindings for both sites.

PS > Get-ChildItem IIS:\Sites

Name             ID   State      Physical Path                  Bindings
----             --   -----      -------------                  --------
Site1            1    Started    Path1                          https *:443: sslFlags=0
Site2            2    Stopped    Path2                          http *:3009:

If I do this without attempting to modify any bindings

foreach($site in $config.Sites){
    $physicalPath = Get-Item "$($site.Path)"
    # Create the current site
    New-WebSite -Name "$($site.Name)" -PhysicalPath $physicalPath.FullName
}

I end up with both sites bound to port 80

PS > Get-ChildItem IIS:\Sites

Name             ID   State      Physical Path                  Bindings
----             --   -----      -------------                  --------
Site1            1    Started    Path1                          http *:80:
Site2            2    Stopped    Path2                          http *:80:

What am I doing wrong? Is there a better way? Is this a bug?


Solution

  • Managed to get around Remove-WebBinding by using the first binding when creating the site and then iterating through any remaining bindings.

    foreach($site in $config.Sites){
        $physicalPath = Get-Item "$($site.Path)"
    
        $defaultBinding = ($site.Bindings | Select-Object -First 1)
    
        # Use Splatting
        $newWebSiteParams = @{
            Name = $site.Name;
            PhysicalPath = $physicalPath.FullName;
            ApplicationPool = $site.ApplicationPool
            Port = $defaultBinding.Port
            SSL = $defaultBinding.Protocol -eq 'https'
        }
    
        # Create the current site with command splatting
        New-WebSite @newWebSiteParams
    
        ## Add the remaining bindings
        foreach ($binding in ($site.Bindings | Select-Object -Skip 1)){
            New-WebBinding -Name "$($site.Name)" -Protocol $binding.Protocol -Port $binding.Port
        }
    }
    

    Still not sure why Remove-WebBinding appeared to be removing the binding from both sites.