powershell - http 的 Set-ItemProperty 删除 IIS 中网站的现有绑定(bind)

标签 powershell web iis

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

当我查看app2的部署脚本时,我意识到有一个函数可以检查绑定(bind)是否已经存在 - 如果是,只需调用Set-ItemProperty来更新它绑定(bind)或创建一个。这个想法对我来说看起来不错 - 基本上它说创建特定于应用程序的绑定(bind)或更新(如果已经存在)。但我不确定,为什么 httpSet-ItemProperty 删除了 https 绑定(bind)(实际上所有其他绑定(bind)也如 net.tcpnet.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 中已为网站配置的所有现有绑定(bind)

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

最佳答案

它删除所有绑定(bind)的原因是,它会获取您传递给 $Protocol 的所有内容并覆盖 Bindings 属性,该属性是 Bindings 属性的集合em>所有站点的绑定(bind)。

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

New-WebBinding -名称“TestMiddleTierSite”-IP地址“*”-端口 81 -协议(protocol) http

关于powershell - http 的 Set-ItemProperty 删除 IIS 中网站的现有绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54040166/

相关文章:

javascript - IIS 中的 AngularJS 路由

iis - 将 IIS 6 复制到 IIS 7.5

javascript - 如何为网络上的多个页面维护 Firebase Auth?

javascript - 为什么关闭 Charles 应用程序后无法访问网站?

azure - 从 AzureAD 安全的 powershell 核心 Azure Function 获取 ClaimsPrincipal/Identity

powershell - PowerShell参数或参数

javascript - 单击按钮后如何停止keydown功能

apache - 将域名指向Tomcat

powershell - 如何防止控制台标题被其他应用程序更改

powershell - 为什么$(Write-Host)的子表达式输出到字符串的开头?