我正在尝试更新特定网站上的http绑定,我可以这样做:
Set-ItemProperty "IIS:\Sites\SiteName" -Name bindings -Value @{protocol="http";bindingInformation=*:80:hostname.site.net}
我遇到的问题是这个命令完全取代了绑定信息,所以如果有一个https绑定,那么它会被Set-ItemProperty删除。
有没有人知道如何更新像HTTP这样的特定绑定而不必删除其他绑定或重新创建整个绑定字符串?
答案 0 :(得分:17)
在网站的绑定集合中更新绑定的步骤,该过程实际上是获取网站的绑定集合,修改特定绑定,然后再次设置整个集合。
Function ReplaceWebsiteBinding {
Param(
[string] $sitename,
[string] $oldBinding,
[string] $newValue
);
import-module webadministration;
$wsbindings = (Get-ItemProperty -Path "IIS:\Sites\$sitename" -Name Bindings)
for($i=0;$i -lt ($wsbindings.Collection).length;$i++){
if((($wsbindings.Collection[$i]).bindingInformation).Contains($oldBinding)){
($wsbindings.Collection[$i]).bindingInformation = $newValue;
}
}
Set-ItemProperty -Path "IIS:\Sites\$sitename" -Name Bindings -Value $wsbindings
}
ReplaceWebsiteBinding "Default Web Site" "*:80:" "192.168.1.101:80:SomeHostHeader.domain";
[注:编辑20131016:清理答案以便于查看] [注:编辑20160817:更正了参数变量类型定义]
答案 1 :(得分:15)
这是另一种语法形式。我更喜欢这个,因为它看起来更自然。如果您尚未加载web管理PS模块,请先导入(import-module webadministration
)。
New-WebBinding -name test03 -port 443 -Protocol https -HostHeader test03.int -IPAddress "*"
答案 2 :(得分:4)
这是我最近写的一个Powershell脚本,可以根据你的需要进行调整:
# Updates IIS bindings across all sites by replacing all occurrences
# of $searchString for $replaceString in the binding host header.
# Note that the search and replace is case insensitive.
$searchString = "ovh-ws0"
$replaceString = "ovh-ws1"
foreach ($website in Get-Website) {
"Site: {0}" -f $website.name
$bindings = Get-WebBinding -Name $website.name
foreach ($binding in $website.bindings.Collection) {
$bindingInfo = $binding.bindingInformation
" Binding: {0}" -f $bindingInfo
if ($bindingInfo -imatch $searchString) {
$oldhost = $bindingInfo.Split(':')[-1]
$newhost = $oldhost -ireplace $searchString, $replaceString
" Updating host: {0} ---> {1}" -f $oldhost, $newhost
Set-WebBinding -Name $website.name -BindingInformation $bindingInfo -PropertyName "HostHeader" -Value $newhost
}
}
}
这是上面脚本的示例输出:
Site: alpha
Binding: 100.101.102.103:80:alpha.redacted.com
Binding: 100.101.102.103:80:ovh-ws0-alpha.redacted.com
Updating host: ovh-ws0-alpha.redacted.com ---> ovh-ws1-alpha.redacted.com
Binding: 100.101.102.103:443:ovh-ws0-alpha.redacted.com
Updating host: ovh-ws0-alpha.redacted.com ---> ovh-ws1-alpha.redacted.com
Binding: 100.101.102.103:443:alpha.redacted.com
Site: beta
(etc)
Site: release
(etc)
当然,脚本可以适用于以其他方式修改绑定。例如,以下脚本将更新IP地址:
# Updates IIS bindings across all sites by replacing all IP addresses from $oldIP to $newIP.
$oldIP = "100.101.102.103"
$newIP = "*"
foreach ($website in Get-Website) {
"Site: {0}" -f $website.name
$bindings = Get-WebBinding -Name $website.name
foreach ($binding in $website.bindings.Collection) {
$bindingInfo = $binding.bindingInformation
" Binding: {0}" -f $bindingInfo
if ($bindingInfo -imatch $oldIP) {
" Updating IP: {0} ---> {1}" -f $oldIP, $newIP
Set-WebBinding -Name $website.name -BindingInformation $bindingInfo -PropertyName "IPAddress" -Value $newIP
}
}
}
答案 3 :(得分:2)
Set-WebBinding -Name'默认网站'-BindingInformation“*:80:” - 属性名称端口-Value 12
Subrat
答案 4 :(得分:1)
使用“New-ItemProperty”添加使用“Set-ItemProperty”来更新现有的。
答案 5 :(得分:1)
我想在不清除 SSL 证书和其他设置的情况下更新现有绑定。
WebAdministration 模块中的 Set-WebBinding 对我有用
例如
Set-WebBinding -PropertyName HostHeader -Value newhostnamevalue.site.net -HostHeader hostname.site.net -IPAddress * -Name MyWebSite -Port 80
对于语法,PropertyName 表示要更新的绑定部分,Value 表示要更新的内容