Last active
December 22, 2023 18:25
-
-
Save mesuttalebi/e96fa3d18b2edf43ab87b8acbac37730 to your computer and use it in GitHub Desktop.
IIS binding switch for Blue-Green Deployment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 $blueSiteName to $greenSiteName ..." | |
# Import the WebAdministration module | |
Import-Module IISAdministration | |
# Display existing bindings for Blue environment | |
Write-Host "Existing bindings for $blueSiteName :" | |
$existingBindings = Get-IISSiteBinding -Name $blueSiteName | |
# Create a copy of the collection to avoid "Collection was modified" error | |
$bindingsCopy = @() | |
foreach ($binding in $existingBindings) { | |
$bindingInfo = $binding.BindingInformation | |
# Do not copy blue website's blue tagged binding, because we want to keep it to prevent blue website stopped by IIS. | |
if ($bindingInfo -notlike "*blue*") { | |
$bindingsCopy += $binding | |
} | |
} | |
# Remove existing bindings for Blue environment | |
foreach ($binding in $bindingsCopy) { | |
$bindingInfo = $binding.BindingInformation | |
Write-Host "Removing binding: $bindingInfo " | |
Remove-IISSiteBinding -Name $blueSiteName -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 $greenSiteName -BindingInformation $binding.BindingInformation -Protocol http | |
} | |
elseif ($binding.Protocol -eq "https") { | |
New-IISSiteBinding -Name $greenSiteName -BindingInformation $binding.BindingInformation -CertificateThumbPrint $certificateThumbprint -CertStoreLocation "Cert:\LocalMachine\$certificateStore" -Protocol https | |
} | |
} | |
Reset-IISServerManager -Confirm:$false | |
Write-Host "Traffic switched from $blueSiteName to $greenSiteName." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment