Remove-WebBindings 从所有 IIS 站点中删除绑定

Remove-WebBindings removing binding from all IIS sites

我正在使用 WebAdministration 模块创建一个 Powershell 脚本来创建几个站点并自动添加绑定

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

不过,当我这样做时,我没有绑定 Site1 端口 80。 Remove-WebBinding -Name $site.Name -Port 80 -Protocol "http" 似乎正在删除两个站点的绑定。

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:

如果我在不尝试修改任何绑定的情况下执行此操作

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

我最终将两个站点都绑定到端口 80

PS > Get-ChildItem IIS:\Sites

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

我做错了什么?有没有更好的办法?这是一个错误吗?

通过在创建站点时使用第一个绑定然后遍历所有剩余的绑定设法绕过了 Remove-WebBinding

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

仍然不确定为什么 Remove-WebBinding 似乎要从两个站点中删除绑定。

我见过类似的行为。我没有尝试显式查找和删除绑定条目,而是发现使用管道引用已识别的对象更成功。

例如:

Get-Website -Name "$($site.Name)" | Get-WebBinding -Protocol "http" -Port 80 | Remove-WebBinding