Powershell Set-ItemProperty for http删除IIS中网站的现有绑定

Powershell Set-ItemProperty for http删除IIS中网站的现有绑定,powershell,web,iis,Powershell,Web,Iis,我的一个应用程序(比如说app1)在IIS的网站下运行,在部署期间创建了https绑定。但是,当最近通过power shell脚本部署同一网站下的另一个应用程序(例如app2)时,它删除了先前添加的https绑定,并中断了app1 当我查看app2的部署脚本时,我意识到有一个函数可以检查绑定是否已经存在——如果是,只需调用Set ItemProperty来更新该绑定,或者创建一个绑定。这个想法在我看来很好——基本上它说创建特定于应用程序的绑定或更新(如果已经存在)。但我不确定,为什么为http设

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

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

下面是该部署脚本中的
功能

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

它删除所有绑定的原因是,它接受您传递给
$Protocol
的任何内容,并重写
绑定
属性,该属性是站点所有绑定的集合

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


新的WebBinding-名称“TestMiddleTierSite”-IPAddress“*”-端口81-协议http

而@boxdog的答案是正确的,值得推荐的:可以使用*-ItemProperty和IIS:PSDrive添加绑定。不要使用设置-ItemProperty,而是使用新建-ItemProperty将新属性添加到集合中:

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