Skip to content

Instantly share code, notes, and snippets.

@obsti8383
Last active March 19, 2023 15:44
Show Gist options
  • Save obsti8383/b79f5b3750e431c43d7a54dfa626a8a2 to your computer and use it in GitHub Desktop.
Save obsti8383/b79f5b3750e431c43d7a54dfa626a8a2 to your computer and use it in GitHub Desktop.
<#
.SYNOPSIS
Merges two CSV files with common attribute set
.EXAMPLE
./mergeCompareCsv.ps1 ./users1.csv ./users2.csv -outfile merged.csv -Delimiter ';'
#>
[CmdletBinding()]
Param (
[parameter(Position = 0, Mandatory = $true)]
[String]$file1,
[parameter(Position = 1, Mandatory = $true)]
[String]$file2,
[parameter()]
[String]$outfile,
[parameter()]
[char]$Delimiter = ',',
[parameter()]
[String]$htmlReportFile
)
$ErrorActionPreference = "Stop"
# func to create merged object, edit for your use case / file headers
function mergeObjs {
[CmdletBinding()]
param(
$o1,
$o2
)
$obj = [pscustomobject]@{
id = $null -ne $o1 ? $o1.id : $o2.id
firstName = $null -ne $o1.firstName ? $o1.firstName : $o2.firstName
lastName = $null -ne $o1.lastName ? $o1.lastName : $o2.lastName
existsIn1 = $null -ne $o1 ? $true : $false
existsIn2 = $null -ne $o2 ? $true : $false
birthday1 = $o1.birthday
birthday2 = $o2.birthday
birthdayEqual = $o1.birthday -eq $o2.birthday
}
return $obj
}
# Import CSV files
$csv1 = Import-Csv -Encoding 'utf8' -Delimiter $Delimiter -Path $file1
$csv2 = Import-Csv -Encoding 'utf8' -Delimiter $Delimiter -Path $file2
# create maps with id as index
$m1 = @{}
foreach ( $c1 in $csv1 ) {
$m1[$c1.id] = $c1
}
$m2 = @{}
foreach ( $c2 in $csv2 ) {
$m2[$c2.id] = $c2
}
# merge with m1 as master
$merged = @()
foreach ( $i in $m1.Values ) {
$merged += mergeObjs -o1 $i -o2 $m2[$i.id]
}
# add missing entries from m2
foreach ( $i in $m2.Values ) {
if ( $null -eq $m1[$i.id] ) {
$merged += mergeObjs -o1 $null -o2 $i
}
}
# write to CSV
if ( $outfile ) {
$merged | Export-Csv -Delimiter $Delimiter -Path $outfile -Encoding 'utf8' -UseQuotes AsNeeded
}
# print to output
$merged | ft
# Generate Report with Module PSWriteHTML (Install with: Install-Module -Name PSWriteHTML -Scope CurrentUser -Force)
if ($htmlReportFile -and (Get-Module -ListAvailable -Name PSWriteHTML) ) {
$table = { New-HTMLTable -DataTable $merged -HideFooter -PagingLength 100 -Html {
New-TableCondition -Name 'birthdayEqual' -ComparisonType string -Operator eq -Value 'False' -Color Orange }
}
New-HTML -HtmlData $table -FilePath $htmlReportFile -ShowHTML -TitleText 'Merged'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment