PowerShell的更新IIS绑定
我试图更新HTTP特定网站上的结合,我可以用做:PowerShell的更新IIS绑定
Set-ItemProperty "IIS:\Sites\SiteName" -Name bindings -Value @{protocol="http";bindingInformation=*:80:hostname.site.net}
我有但问题是,这个命令完全取代绑定信息,以便如果有一个https绑定,那么它将被Set-ItemProperty删除。
有没有人知道只更新像HTTP一样的特定绑定的方式,而不必删除其他人或重新创建整个绑定字符串?
更新网站绑定集合中绑定的步骤,该过程实际上是获取网站的绑定集合,修改特定的绑定,然后再次设置整个集合。
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:修正PARAM变量类型定义]
这里的另一种语法形式。我更喜欢这个,因为它看起来更自然。如果您尚未加载Web管理PS模块,请首先导入(import-module webadministration
)。
New-WebBinding -name test03 -port 443 -Protocol https -HostHeader test03.int -IPAddress "*"
您可以使用此命令添加SSL证书吗? – 2015-01-26 09:14:22
是的 - 这里有一些关于如何做到这一点的更多信息:http://www.iis.net/learn/manage/powershell/powershell-snap-in-configuring-ssl-with-the-iis-powershell-snap-在 – northben 2015-01-26 23:29:35
请注意,这个commandlet是随Windows Server 2012一起推出的。如果你仍然在Server 2008上,你将不得不使用JohnnyG的解决方案。 – JamesQMurphy 2015-02-11 15:48:30
设置-WebBinding -Name '默认Web站点' -BindingInformation “*:80:” -propertyname港 - 数值12
Subrat
你应该尝试在你的答案中包含更多的解释。 – kddeisz 2014-01-08 20:27:51
使用 “新ItemProperty” 添加应用“ Set-ItemProperty“来更新一个现有的。
这里是一个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
}
}
}
我不认为这个解决方案是不够好,因为它会删除所有绑定,如果你有一个以上的,只是留给你指定的一个。 – 2013-05-12 11:39:45
我不同意它完全回答这个问题,但我认为这与使用针对IIS的powershell一样好。我想看到的是'$ b = Get-WebBinding; $ b.SetAttribute('Port',9999); $ b.Save;'但那不行。我不能删除downvote,除非你改变答案(!)......怪异。 – 2013-05-13 13:21:55
通过改变提供了勺子解决方案。请检查你的downvote。 – JonnyG 2013-05-28 12:36:28