代码之家  ›  专栏  ›  技术社区  ›  rahulaga-msft

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

  •  2
  • rahulaga-msft  · 技术社区  · 7 年前

    我的一份申请书(比如 app1 )在IIS中的网站下运行已创建 https 部署期间的绑定。但是当另一个应用程序(例如 app2 )在最近通过Powershell脚本部署的同一个网站下,它删除了先前添加的内容 https 附件1 .

    当我查看 ,我意识到有一个函数可以检查绑定是否已经存在-如果是,只需调用 Set-ItemProperty 设置项目属性 对于 http 远离的 绑定(事实上,所有其他 net.tcp , net.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
        }
    

    SetBindingsIIS -WebsiteName "TestMiddleTierSite" -protocol @{Protocol="http";BindingInformation=":81:"}
    
    2 回复  |  直到 7 年前
        1
  •  2
  •   boxdog    7 年前

    它删除所有绑定的原因是它接受您传递给它的任何内容 $Protocol 写得太多了 Bindings 属性,它是 全部的 站点的绑定。

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

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

        2
  •  2
  •   Gert Jan Kraaijeveld    7 年前

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

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