首页 > 解决方案 > Set-ItemProperty for http 删除 IIS 中网站的现有绑定

问题描述

app1我在 IIS 网站下运行的一个应用程序(比如)https在部署期间创建了绑定。但是,当app2最近通过 power shell 脚本部署同一网站下的另一个应用程序(例如)时,它删除了先前添加的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:"}

标签: powershellwebiis

解决方案


它删除所有绑定的原因是它正在获取您传递给的任何内容$Protocol并覆盖属性,该属性是站点所有绑定Bindings的集合。

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

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


推荐阅读