【发布时间】:2019-01-04 13:45:36
【问题描述】:
我的一个应用程序(比如app1)在IIS 的网站下运行,在部署期间创建了https 绑定。但是,当最近通过 power shell 脚本部署同一网站下的另一个应用程序(例如 app2)时,它删除了之前添加的 https 绑定并破坏了 app1。
当我查看 app2 的部署脚本时,我意识到有一个函数可以检查绑定是否已经存在 - 如果是,只需调用 Set-ItemProperty 来更新该绑定或创建一个。这个想法对我来说看起来不错 - 基本上它说创建特定于应用程序的绑定或更新(如果已经存在)。但我不确定,为什么 Set-ItemProperty 为 http 删除了 https 绑定(实际上所有其他人以及 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
}
运行此函数只会删除已在 IIS 中为网站配置的所有现有绑定
SetBindingsIIS -WebsiteName "TestMiddleTierSite" -protocol @{Protocol="http";BindingInformation=":81:"}
【问题讨论】:
标签: powershell web iis