【问题标题】:Set-ItemProperty for http removes existing bindings for website in IISSet-ItemProperty for http 删除 IIS 中网站的现有绑定
【发布时间】:2019-01-04 13:45:36
【问题描述】:

我的一个应用程序(比如app1)在IIS 的网站下运行,在部署期间创建了https 绑定。但是,当最近通过 power shell 脚本部署同一网站下的另一个应用程序(例如 app2)时,它删除了之前添加的 https 绑定并破坏了 app1

当我查看 app2 的部署脚本时,我意识到有一个函数可以检查绑定是否已经存在 - 如果是,只需调用 Set-ItemProperty 来更新该绑定或创建一个。这个想法对我来说看起来不错 - 基本上它说创建特定于应用程序的绑定或更新(如果已经存在)。但我不确定,为什么 Set-ItemPropertyhttp 删除了 https 绑定(实际上所有其他人以及 net.tcpnet.pipe 等)

下面是来自该部署脚本的function

Import-Module -Name WebAdministration
    function SetBindingsIIS
    {
    param
    (
       [Parameter(Mandatory)]
       [ValidateNotNullOrEmpty()]
       [string]$WebsiteName,
       [HashTable]$protocol
    )
    $Status=$null
    $GetProtocolName= $protocol["Protocol"]
    $BindingsCollection=Get-ItemProperty -Path "IIS:\Sites\$WebsiteName" -Name Bindings 
    $ProtocolExists=$BindingsCollection.Collection | Where-Object{$_.protocol -eq $GetProtocolName}
        Try
        {
            if($ProtocolExists -eq $null)
            {
                New-ItemProperty -Path IIS:\Sites\$WebsiteName -Name Bindings -Value $protocol -Force
            }
            else
            {
                Set-ItemProperty -Path "IIS:\Sites\$WebsiteName" -Name Bindings -Value $protocol -Force
            }
            $Status="Success"
        }
        Catch
        {
            $ErrorMessage=$_.Exception.Message        
            $Status="Error in Add/Update bindings : $ErrorMessage"
        }

        return $Status
    }

运行此函数只会删除已在 IIS 中为网站配置的所有现有绑定

SetBindingsIIS -WebsiteName "TestMiddleTierSite" -protocol @{Protocol="http";BindingInformation=":81:"}

【问题讨论】:

    标签: powershell web iis


    【解决方案1】:

    它删除所有绑定的原因是它会将您传递给$Protocol 的任何内容都覆盖并覆盖Bindings 属性,该属性是所有的绑定的集合网站。

    您应该使用 IIS 附带的 WebAdministration 模块来执行此操作,而不是使用通用项 cmdlet。它包含各种有用的 cmdlet,包括 Set-WebBindingNew-WebBinding。例如:

    New-WebBinding -Name "TestMiddleTierSite" -IPAddress "*" -Port 81 -Protocol http

    【讨论】:

      【解决方案2】:

      虽然@boxdog 的回答是正确且值得推荐的:可以使用 *-ItemProperty 和 IIS:PSDrive 添加绑定。只是不要使用 Set-ItemProperty,而是使用 New-ItemProperty 向集合添加新属性:

      New-ItemProperty 'IIS:\Sites\Default Web Site' -Name bindings -Value @{protocol='http'; bindingInformation='*:81:'}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-01-06
        • 1970-01-01
        • 2016-02-08
        • 1970-01-01
        • 1970-01-01
        • 2020-06-02
        • 1970-01-01
        • 2011-03-31
        相关资源
        最近更新 更多