我的一个应用程序(例如 app1
)在 IIS 网站下运行,在部署期间创建了 https
绑定(bind)。然而,当最近通过 Power shell 脚本部署同一网站下的另一个应用程序(例如 app2
)时,它删除了之前添加的 https
绑定(bind)并破坏了 app1
。
当我查看app2
的部署脚本时,我意识到有一个函数可以检查绑定(bind)是否已经存在 - 如果是,只需调用Set-ItemProperty
来更新它绑定(bind)或创建一个。这个想法对我来说看起来不错 - 基本上它说创建特定于应用程序的绑定(bind)或更新(如果已经存在)。但我不确定,为什么 http
的 Set-ItemProperty
删除了 https
绑定(bind)(实际上所有其他绑定(bind)也如 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 中已为网站配置的所有现有绑定(bind)
SetBindingsIIS -WebsiteName "TestMiddleTierSite" -protocol @{Protocol="http";BindingInformation=":81:"}
最佳答案
它删除所有绑定(bind)的原因是,它会获取您传递给 $Protocol
的所有内容并覆盖 Bindings
属性,该属性是 Bindings
属性的集合em>所有站点的绑定(bind)。
您应该使用 IIS 附带的 WebAdministration
模块来执行此操作,而不是使用通用项目 cmdlet。它包含各种有用的 cmdlet,包括 Set-WebBinding
和 New-WebBinding
。例如:
New-WebBinding -名称“TestMiddleTierSite”-IP地址“*”-端口 81 -协议(protocol) http
关于powershell - http 的 Set-ItemProperty 删除 IIS 中网站的现有绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54040166/