Last active
July 8, 2024 05:13
-
-
Save davidlu1001/53234f3336efdbed4143afcb8d670994 to your computer and use it in GitHub Desktop.
This file contains 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
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory = $false)] | |
[string]$OldNewDnsFile = "dns_old_new.csv", | |
[Parameter(Mandatory = $false)] | |
[string[]]$ComputerNames = @("localhost"), | |
[Parameter(Mandatory = $false)] | |
[string]$OutputFile = "updateIISBindingsResult.csv", | |
[Parameter(Mandatory = $false)] | |
[switch]$DryRun | |
) | |
# Import required modules | |
Import-Module WebAdministration -ErrorAction SilentlyContinue | |
function Update-IISBindings { | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory = $true)] | |
[string]$ComputerName, | |
[Parameter(Mandatory = $true)] | |
[array]$DnsMapping, | |
[Parameter(Mandatory = $false)] | |
[switch]$DryRun | |
) | |
try { | |
$scriptBlock = { | |
param($DnsMapping, $DryRun) | |
Import-Module WebAdministration -ErrorAction SilentlyContinue | |
$results = @() | |
Get-WebSite | ForEach-Object { | |
$site = $_ | |
$siteName = $site.Name | |
$appPool = $site.applicationPool | |
$sitePath = $site.PhysicalPath | |
$originalBindings = $site.Bindings.Collection | ForEach-Object { | |
"{0} {1} sslFlags={2}" -f $_.Protocol, $_.BindingInformation, $_.sslFlags | |
} | |
Write-Verbose "Processing site: $siteName" | |
foreach ($mapping in $DnsMapping) { | |
$oldValue = $mapping.oldValue | |
$newValue = $mapping.newValue | |
$bindingsUpdated = $false | |
$updatedBindings = @() | |
foreach ($binding in $site.Bindings.Collection) { | |
$bindingParts = $binding.BindingInformation -split ':' | |
$ip, $port, $hostHeader = $bindingParts | |
if ($hostHeader -like "*$oldValue*") { | |
$newHostHeader = $hostHeader -replace [regex]::Escape($oldValue), $newValue | |
$newBindingInfo = "{0}:{1}:{2}" -f $ip, $port, $newHostHeader | |
Write-Verbose "Match found for site '$siteName':" | |
Write-Verbose " Old: $($binding.Protocol) $($binding.BindingInformation) sslFlags=$($binding.sslFlags)" | |
Write-Verbose " New: $($binding.Protocol) $newBindingInfo sslFlags=$($binding.sslFlags)" | |
$bindingsUpdated = $true | |
if (-not $DryRun) { | |
$updatedBindings += @{ | |
Protocol = $binding.Protocol | |
BindingInformation = $newBindingInfo | |
SslFlags = $binding.SslFlags | |
} | |
} else { | |
$updatedBindings += $binding | |
} | |
} else { | |
$updatedBindings += $binding | |
} | |
} | |
$result = if ($bindingsUpdated) { | |
if ($DryRun) { | |
Write-Verbose "[DryRun] Would update bindings for site '$siteName'" | |
"WillUpdate" | |
} else { | |
Set-ItemProperty -Path "IIS:\Sites\$siteName" -Name Bindings -Value $updatedBindings | |
Write-Verbose "Bindings updated for site '$siteName'" | |
"Updated" | |
} | |
} else { | |
Write-Verbose "No changes needed for site '$siteName'" | |
"NoChangeNeeded" | |
} | |
$updatedBindingsString = if ($bindingsUpdated -and -not $DryRun) { | |
($updatedBindings | ForEach-Object { | |
"{0} {1} sslFlags={2}" -f $_.Protocol, $_.BindingInformation, $_.sslFlags | |
}) -join ", " | |
} else { | |
"N/A" | |
} | |
$results += [PSCustomObject]@{ | |
ComputerName = $env:COMPUTERNAME | |
IISSite = $siteName | |
AppPool = $appPool | |
SitePath = $sitePath | |
OriginalBindings = ($originalBindings -join ", ") | |
UpdatedBindings = $updatedBindingsString | |
OldValue = $oldValue | |
NewValue = $newValue | |
Result = $result | |
} | |
} | |
} | |
return $results | |
} | |
$invokeParams = @{ | |
ScriptBlock = $scriptBlock | |
ArgumentList = $DnsMapping, $DryRun | |
ErrorAction = 'Stop' | |
} | |
if ($ComputerName -ne "localhost" -and $ComputerName -ne $env:COMPUTERNAME) { | |
$invokeParams['ComputerName'] = $ComputerName | |
Invoke-Command @invokeParams | |
} else { | |
& $scriptBlock $DnsMapping $DryRun | |
} | |
} | |
catch { | |
Write-Error "Error processing computer '$ComputerName': $_" | |
} | |
} | |
# Main script execution | |
try { | |
# Validate CSV file existence | |
if (-not (Test-Path $OldNewDnsFile)) { | |
throw "The specified DNS mapping file does not exist: $OldNewDnsFile" | |
} | |
# Read CSV file | |
$dnsMapping = Import-Csv -Path $OldNewDnsFile -ErrorAction Stop | |
# Validate CSV content | |
if (-not ($dnsMapping | Get-Member -Name "oldValue" -MemberType NoteProperty) -or | |
-not ($dnsMapping | Get-Member -Name "newValue" -MemberType NoteProperty)) { | |
throw "The CSV file must contain 'oldValue' and 'newValue' columns." | |
} | |
# Initialize results array | |
$allResults = @() | |
foreach ($computer in $ComputerNames) { | |
Write-Verbose "Processing computer: $computer" | |
$results = Update-IISBindings -ComputerName $computer -DnsMapping $dnsMapping -DryRun:$DryRun -Verbose | |
$allResults += $results | |
} | |
# Export results to CSV | |
$allResults | Export-Csv -Path $OutputFile -NoTypeInformation | |
if ($DryRun) { | |
Write-Host "Dry run completed. No changes were made to IIS bindings." | |
} else { | |
Write-Host "IIS binding update process completed." | |
} | |
Write-Host "Results exported to $OutputFile" | |
} | |
catch { | |
Write-Error "An error occurred: $_" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment