I'm trying to update the http binding on a particular site which I can do with:
Set-ItemProperty "IIS:\Sites\SiteName" -Name bindings -Value @{protocol="http";bindingInformation=*:80:hostname.site.net}
The problem I'm having though is that this command completely replaces the binding information so if there is a https binding then it gets removed with Set-ItemProperty.
Does anyone know a way of just updating a specific binding like HTTP without having to remove the others or recreate the whole binding string?
On the Windows Start menu, on the right side, click Administrative Tools > Internet Information Services (IIS) Manager. In IIS Manager, under Connections, expand your server name, and then expand Sites. Right-click on a website, and then click Edit Bindings. In the Site Bindings window, click Add.
The Set-WebConfigurationProperty cmdlet changes the value of an Internet Information Services (IIS) configuration property. Specify the element as a configuration section or an XPath query. Globbing, or the use of wildcards, is supported.
The steps to UPDATE a binding in a website's binding collection, the process is actually to Get the website's binding collection, modify the specific binding, and then set the whole collection again.
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";
[note: Edited 20131016: Clean up answer for easier viewing ] [note: Edited 20160817: Corrected param variable type definitions ]
Here's another syntax form. I prefer this one because it seems more natural. If you don't already have the webadministration PS module loaded, import that first (import-module webadministration
).
New-WebBinding -name test03 -port 443 -Protocol https -HostHeader test03.int -IPAddress "*"
Here is a Powershell script I wrote recently that can be adapted to do what you want:
# 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
}
}
}
And this is a sample output from the script above:
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)
Of course, the script can be adapted to do modify bindings in other ways. For example, the following script will update the IP addresses:
# 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
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With