【问题标题】:Remove-WebBindings removing binding from all IIS sitesRemove-WebBindings 从所有 IIS 站点中删除绑定
【发布时间】:2020-01-06 10:43:11
【问题描述】:

我正在使用 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
    }
}

但是,当我这样做时,我在端口 80 Site1 上没有绑定。看起来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:

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

【问题讨论】:

    标签: powershell iis-10


    【解决方案1】:

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

    例如:

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

    【讨论】:

      【解决方案2】:

      通过在创建站点时使用第一个绑定,然后遍历所有剩余的绑定,设法绕过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 似乎要从两个站点上删除绑定。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-20
        • 2020-06-02
        • 2013-05-26
        • 1970-01-01
        相关资源
        最近更新 更多