61 lines
2.2 KiB
PowerShell
61 lines
2.2 KiB
PowerShell
function Remove-DuplicateHostsFileRecords {
|
|
<#
|
|
.SYNOPSIS
|
|
Removes duplicate entries from the hostfile, preferring the longest comment in the case of duplicate entries
|
|
Assumes that a distinct record is IpAddress + Hostname
|
|
|
|
.PARAMETER Records
|
|
A list of two or more records. An empty array or one that has only one element will be returned as-is.
|
|
A list of records of the format @{ Keep = $false; IpAddress = $null; Hostname = $null; Comment = $null; BlankLine = $false; }
|
|
#>
|
|
[CmdletBinding()]
|
|
[OutputType([object[]])]
|
|
param (
|
|
[Object[]]$Records
|
|
)
|
|
|
|
# If we have no records, or only one record, how can we remove duplicates?
|
|
if ((Test-IsCollectionNullOrEmpty $Records) -or ($Records.Count -eq 1)) {
|
|
return $Records
|
|
}
|
|
|
|
$hash = @{}
|
|
foreach ($record in $Records) {
|
|
if ([string]::IsNullOrWhiteSpace($record.IpAddress) -and [string]::IsNullOrWhiteSpace($record.Hostname)) {
|
|
continue
|
|
} else {
|
|
$indexString = "$($record.IpAddress)$($record.Hostname)".ToLower()
|
|
$formattedRecord = Format-HostsFileRecord -Record $record
|
|
if ($null -eq $hash[$indexString]) {
|
|
$hash[$indexString] = $formattedRecord
|
|
}
|
|
if ($formattedRecord.Length -gt $hash[$indexString].Length) {
|
|
$hash[$indexString] = $formattedRecord
|
|
}
|
|
}
|
|
}
|
|
|
|
$outputRecords = @()
|
|
foreach ($record in $Records) {
|
|
if ([string]::IsNullOrWhiteSpace($record.IpAddress) -and [string]::IsNullOrWhiteSpace($record.Hostname)) {
|
|
$outputRecords += $record
|
|
} else {
|
|
$indexString = "$($record.IpAddress)$($record.Hostname)".ToLower()
|
|
$formattedRecord = Format-HostsFileRecord -Record $record
|
|
|
|
# There should have been a record here, so we must have removed it
|
|
if ($null -eq $hash[$indexString]) {
|
|
continue
|
|
}
|
|
|
|
if ($formattedRecord.Length -eq $hash[$indexString].Length) {
|
|
$outputRecords += $record
|
|
|
|
# Only keep the first one that matches, get rid of all the rest by hitting the above fork
|
|
$hash[$indexString] = $null
|
|
}
|
|
}
|
|
}
|
|
|
|
return $outputRecords
|
|
} |