powershell - Set-ItemProperty for http 删除 IIS 中网站的现有绑定
问题描述
app1
我在 IIS 网站下运行的一个应用程序(比如)https
在部署期间创建了绑定。但是,当app2
最近通过 power shell 脚本部署同一网站下的另一个应用程序(例如)时,它删除了先前添加的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:"}
解决方案
它删除所有绑定的原因是它正在获取您传递给的任何内容$Protocol
并覆盖属性,该属性是站点所有绑定Bindings
的集合。
您应该使用WebAdministration
IIS 附带的模块而不是通用项 cmdlet 来执行此操作。它包含各种有用的 cmdlet,包括Set-WebBinding
和New-WebBinding
. 例如:
New-WebBinding -Name "TestMiddleTierSite" -IPAddress "*" -Port 81 -Protocol http
推荐阅读
- netlogo - 如何在 NetLogo 中创建条形图,其中条形并排
- html - 导航文本越过 html 中的标题容器
- javascript - jquery预览图像不起作用
- python - 使用 C 错误扩展 Python:分段错误:Python 中的 11 [MacOS X]
- r - 加入来自不同季节/年份的两个数据集,与运动相关(例如,NHL,1991 和 1992 赛季)
- node.js - 为什么 Twitter API 调用在 Node.js 中工作正常,但在 ReactJS 中导致 400 错误?
- javascript - 初学者 JavaScript - 结果未在结果框中发布
- python-2.7 - Python 2.7 和 PrettyTables
- ios - Swift/iOS - 在后台每 10 秒运行一次代码
- javascript - JS 类方法未执行(Rails active_storage)