Skip to content

Instantly share code, notes, and snippets.

@davidlu1001
Last active June 25, 2024 09:21
Show Gist options
  • Save davidlu1001/f9c844aa1a420513abef3ec16b124dd9 to your computer and use it in GitHub Desktop.
Save davidlu1001/f9c844aa1a420513abef3ec16b124dd9 to your computer and use it in GitHub Desktop.
Update IIS Bindings
[CmdletBinding()]
param (
[Parameter(Mandatory=$false)]
[string]$matchPattern = "\.co\.nz",
[Parameter(Mandatory=$false)]
[string]$originalString = "dev",
[Parameter(Mandatory=$false)]
[string]$targetString = "dev2",
[Parameter(Mandatory=$false)]
[string[]]$computerNames = @("localhost"),
[Parameter(Mandatory=$false)]
[System.Management.Automation.PSCredential]$Credential
)
# Import the IIS module
Import-Module WebAdministration -ErrorAction SilentlyContinue
function Update-Bindings {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[string]$siteName,
[Parameter(Mandatory=$true)]
[string]$matchPattern,
[Parameter(Mandatory=$true)]
[string]$originalString,
[Parameter(Mandatory=$true)]
[string]$targetString
)
try {
$site = Get-WebSite -Name $siteName -ErrorAction SilentlyContinue
$updatedBindings = @()
$bindingsUpdated = $false
foreach ($binding in $site.Bindings.Collection) {
$bindingParts = $binding.BindingInformation -split ':'
$ip, $port, $hostHeader = $bindingParts
if ($hostHeader -match $matchPattern) {
$newHostHeader = $hostHeader -replace "(?i)$originalString", {
if ($_.Value -cmatch "[A-Z]") { $targetString.ToUpper() } else { $targetString.ToLower() }
}
if ($newHostHeader -ne $hostHeader) {
$newBindingInfo = "{0}:{1}:{2}" -f $ip, $port, $newHostHeader
Write-Verbose "Updating binding for site '$siteName':`n Old: $($binding.Protocol) $($binding.BindingInformation)`n New: $($binding.Protocol) $newBindingInfo"
$updatedBindings += @{
Protocol = $binding.Protocol
BindingInformation = $newBindingInfo
SslFlags = $binding.SslFlags
}
$bindingsUpdated = $true
} else {
$updatedBindings += $binding
}
} else {
$updatedBindings += $binding
}
}
if ($bindingsUpdated) {
Set-ItemProperty -Path "IIS:\Sites\$siteName" -Name Bindings -Value $updatedBindings
Write-Verbose "Bindings updated for site '$siteName'"
} else {
Write-Verbose "No bindings updated for site '$siteName'"
}
}
catch {
Write-Error "Error updating bindings for site '$siteName': $_"
}
}
function Update-IISBindings {
[CmdletBinding()]
param (
[Parameter(Mandatory=$true)]
[string]$ComputerName,
[Parameter(Mandatory=$true)]
[string]$MatchPattern,
[Parameter(Mandatory=$true)]
[string]$OriginalString,
[Parameter(Mandatory=$true)]
[string]$TargetString,
[Parameter(Mandatory=$false)]
[System.Management.Automation.PSCredential]$Credential
)
try {
$scriptBlock = {
param($MatchPattern, $OriginalString, $TargetString)
Import-Module WebAdministration -ErrorAction SilentlyContinue
Get-WebSite | ForEach-Object {
Update-Bindings -siteName $_.Name -matchPattern $MatchPattern -originalString $OriginalString -targetString $TargetString -Verbose
}
}
$invokeParams = @{
ComputerName = $ComputerName
ScriptBlock = $scriptBlock
ArgumentList = $MatchPattern, $OriginalString, $TargetString
ErrorAction = 'SilentlyContinue'
}
if ($ComputerName -ne "localhost" -and $ComputerName -ne $env:COMPUTERNAME) {
if ($Credential) {
$invokeParams['Credential'] = $Credential
}
Invoke-Command @invokeParams
} else {
& $scriptBlock $MatchPattern $OriginalString $TargetString
}
}
catch {
Write-Error "Error processing computer '$ComputerName': $_"
}
}
foreach ($computer in $computerNames) {
Write-Verbose "Processing computer: $computer"
Update-IISBindings -ComputerName $computer -MatchPattern $matchPattern -OriginalString $originalString -TargetString $targetString -Credential $Credential -Verbose
}
Write-Host "Binding update process completed."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment