Skip to content

Instantly share code, notes, and snippets.

@mesuttalebi
Last active December 22, 2023 18:24
Show Gist options
  • Save mesuttalebi/a14b4170e89653d24dd1ba16c08f93c0 to your computer and use it in GitHub Desktop.
Save mesuttalebi/a14b4170e89653d24dd1ba16c08f93c0 to your computer and use it in GitHub Desktop.
switch IIS Binding to achieve Blue-Green Deployment.
param (
[string]$blueSiteName = "testblue.mydomain.com",
[string]$greenSiteName = "testgreen.mydomain.com",
[string]$ipAddress = "192.168.1.108",
[int]$httpPort = 80,
[int]$httpsPort = 443,
[string]$certificateThumbprint = "20437383726bb2e609619ee345825c4854ab0598",
[string]$certificateStore = "My"
)
Write-Host "Traffic switching from $greenSiteName to $blueSiteName ..."
# Import the WebAdministration module
Import-Module IISAdministration
# Display existing bindings for Blue environment
Write-Host "Existing bindings for $greenSiteName :"
$existingBindings = Get-IISSiteBinding -Name $greenSiteName
# Create a copy of the collection to avoid "Collection was modified" error
$bindingsCopy = @()
foreach ($binding in $existingBindings) {
$bindingInfo = $binding.BindingInformation
# Do not copy green website's bindings, because we want keep them.
if ($bindingInfo -notlike "*green*") {
$bindingsCopy += $binding
}
}
# Remove existing bindings for Blue environment
foreach ($binding in $bindingsCopy) {
$bindingInfo = $binding.BindingInformation
Write-Host "Removing binding: $bindingInfo "
Remove-IISSiteBinding -Name $greenSiteName -BindingInformation $bindingInfo -Confirm:$false -Protocol $binding.Protocol -RemoveConfigOnly
}
Reset-IISServerManager -Confirm:$false
# Add bindings for Green environment
foreach ($binding in $bindingsCopy) {
if ($binding.Protocol -eq "http") {
New-IISSiteBinding -Name $blueSiteName -BindingInformation $binding.BindingInformation -Protocol http
}
elseif ($binding.Protocol -eq "https") {
New-IISSiteBinding -Name $blueSiteName -BindingInformation $binding.BindingInformation -CertificateThumbPrint $certificateThumbprint -CertStoreLocation "Cert:\LocalMachine\$certificateStore" -Protocol https
}
}
Reset-IISServerManager -Confirm:$false
Write-Host "Traffic switched from $greenSiteName to $blueSiteName"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment