-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombine-HardwareHashCSV.ps1
More file actions
654 lines (553 loc) · 24.9 KB
/
Combine-HardwareHashCSV.ps1
File metadata and controls
654 lines (553 loc) · 24.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
<#
.SYNOPSIS
Combines multiple hardware hash CSV files into a single file for Windows Autopilot.
.DESCRIPTION
This script safely combines multiple hardware hash CSV files into one consolidated file.
It preserves the CSV structure, validates input files, and prevents corruption of original files.
Features:
- Validates CSV structure before combining
- Preserves proper CSV formatting
- Creates backup of existing output file
- Handles duplicate entries
- Supports custom encoding (UTF-8 with BOM by default for Autopilot)
- Detailed logging and error handling
.PARAMETER SourcePath
Path to the folder containing hardware hash CSV files to combine.
Default: Current directory
.PARAMETER OutputFile
Name or full path of the combined output CSV file.
Default: Combined-HardwareHash.csv in the source path
.PARAMETER Recursive
Search for CSV files recursively in subdirectories.
.PARAMETER RemoveDuplicates
Remove duplicate entries based on Device Serial Number.
.PARAMETER CreateBackup
Create a backup of the output file if it already exists.
Default: $true
.PARAMETER GenerateReport
Automatically generate a detailed processing report.
Default: $true
.EXAMPLE
.\Combine-HardwareHashCSV.ps1
Combines all CSV files in the current directory and generates a report.
.EXAMPLE
.\Combine-HardwareHashCSV.ps1 -SourcePath "C:\HardwareHashes" -OutputFile "Autopilot-All.csv"
Combines all CSV files from specified path with custom output name.
.EXAMPLE
.\Combine-HardwareHashCSV.ps1 -SourcePath "C:\HardwareHashes" -Recursive -RemoveDuplicates
Recursively finds all CSV files and removes duplicates.
.NOTES
Author: Joseph Martin
Date: 2026-02-10
Version: 1.0
#>
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)]
[string]$SourcePath = ".",
[Parameter(Mandatory = $false)]
[string]$OutputFile = "Combined-HardwareHash.csv",
[Parameter(Mandatory = $false)]
[switch]$Recursive,
[Parameter(Mandatory = $false)]
[switch]$RemoveDuplicates,
[Parameter(Mandatory = $false)]
[bool]$CreateBackup = $true,
[Parameter(Mandatory = $false)]
[bool]$GenerateReport = $true
)
# Set strict mode for better error handling
Set-StrictMode -Version Latest
$ErrorActionPreference = "Stop"
# Function to write colored output
function Write-ColorOutput {
param(
[string]$Message,
[string]$Type = "Info"
)
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
switch ($Type) {
"Success" { Write-Host "[$timestamp] [SUCCESS] $Message" -ForegroundColor Green }
"Error" { Write-Host "[$timestamp] [ERROR] $Message" -ForegroundColor Red }
"Warning" { Write-Host "[$timestamp] [WARNING] $Message" -ForegroundColor Yellow }
default { Write-Host "[$timestamp] [INFO] $Message" -ForegroundColor Cyan }
}
}
# Function to validate hardware hash CSV structure
function Test-HardwareHashCSV {
param(
[string]$FilePath
)
$result = @{
IsValid = $false
Status = ""
RecordCount = 0
Reason = ""
}
try {
# Check if file is empty (0 bytes)
$fileSize = (Get-Item $FilePath).Length
if ($fileSize -eq 0) {
Write-ColorOutput "File is empty (0 bytes): $FilePath" -Type "Warning"
$result.Status = "Empty"
$result.Reason = "File is empty (0 bytes)"
return $result
}
# Read file content
$content = Get-Content -Path $FilePath
# Check if file only has header (no data rows)
if ($content.Count -le 1) {
Write-ColorOutput "File has no data rows: $FilePath" -Type "Warning"
$result.Status = "EmptyData"
$result.Reason = "File contains only header, no data rows"
return $result
}
# Read first line to check header
$firstLine = $content[0]
# Expected headers for hardware hash CSV (Windows Autopilot format)
$expectedHeaders = @(
"Device Serial Number,Windows Product ID,Hardware Hash",
"Device Serial Number,Windows Product Id,Hardware Hash", # Alternative casing
"Serial Number,Windows Product ID,Hardware Hash"
)
$isValid = $false
foreach ($header in $expectedHeaders) {
if ($firstLine -like "*$($header.Split(',')[0])*" -and
$firstLine -like "*Hardware Hash*") {
$isValid = $true
break
}
}
if (-not $isValid) {
Write-ColorOutput "File does not have expected hardware hash CSV format: $FilePath" -Type "Warning"
$result.Status = "InvalidFormat"
$result.Reason = "File does not have expected hardware hash CSV format"
return $result
}
# File is valid - count data rows (excluding header)
$result.IsValid = $true
$result.Status = "Valid"
$result.RecordCount = $content.Count - 1
$result.Reason = "Valid hardware hash CSV"
return $result
}
catch {
Write-ColorOutput "Error validating file $FilePath : $_" -Type "Error"
$result.Status = "Error"
$result.Reason = "Error validating file: $_"
return $result
}
}
# Main script execution
try {
Write-ColorOutput "Starting Hardware Hash CSV Combine Process" -Type "Info"
Write-ColorOutput "=========================================" -Type "Info"
# Resolve and validate source path
$sourcePath = Resolve-Path -Path $SourcePath -ErrorAction Stop
Write-ColorOutput "Source Path: $sourcePath" -Type "Info"
# Determine output file path
if ([System.IO.Path]::IsPathRooted($OutputFile)) {
$outputPath = $OutputFile
}
else {
$outputPath = Join-Path -Path $sourcePath -ChildPath $OutputFile
}
Write-ColorOutput "Output File: $outputPath" -Type "Info"
# Get all CSV files from source path
$searchParams = @{
Path = $sourcePath
Filter = "*.csv"
File = $true
}
if ($Recursive) {
$searchParams.Add("Recurse", $true)
}
Write-ColorOutput "Searching for *.csv files in: $sourcePath" -Type "Info"
# First, show all CSV files found (before filtering)
$allCsvFiles = @(Get-ChildItem @searchParams)
Write-ColorOutput "DEBUG: Found $($allCsvFiles.Count) total CSV files" -Type "Info"
if ($allCsvFiles.Count -gt 0) {
Write-ColorOutput "DEBUG: Sample files found:" -Type "Info"
$allCsvFiles | Select-Object -First 5 | ForEach-Object {
Write-ColorOutput " - $($_.Name)" -Type "Info"
}
}
$csvFiles = @($allCsvFiles | Where-Object {
$_.FullName -ne $outputPath # Exclude output file if it exists
})
if ($csvFiles.Count -eq 0) {
Write-ColorOutput "No CSV files found in the specified path: $sourcePath" -Type "Warning"
Write-ColorOutput "Please verify:" -Type "Warning"
Write-ColorOutput " 1. The path exists and is accessible" -Type "Warning"
Write-ColorOutput " 2. The path contains CSV files with .csv extension" -Type "Warning"
Write-ColorOutput " 3. You have permission to read the files" -Type "Warning"
exit 1
}
Write-ColorOutput "Found $($csvFiles.Count) CSV file(s) to process" -Type "Info"
# Validate all CSV files and track results for reporting
Write-ColorOutput "Validating CSV files..." -Type "Info"
$validFiles = @()
$reportData = @{
SuccessfulFiles = [System.Collections.ArrayList]::new()
EmptyFiles = [System.Collections.ArrayList]::new()
EmptyDataFiles = [System.Collections.ArrayList]::new()
InvalidFormatFiles = [System.Collections.ArrayList]::new()
ErrorFiles = [System.Collections.ArrayList]::new()
ProcessingErrors = [System.Collections.ArrayList]::new()
}
foreach ($file in $csvFiles) {
Write-ColorOutput "Checking: $($file.Name)" -Type "Info"
$validationResult = Test-HardwareHashCSV -FilePath $file.FullName
if ($validationResult.IsValid) {
$validFiles += $file
Write-ColorOutput " Valid: $($file.Name) - $($validationResult.RecordCount) record(s)" -Type "Success"
}
else {
# Track different types of issues for reporting
$fileInfo = [PSCustomObject]@{
FileName = $file.Name
FilePath = $file.FullName
Reason = $validationResult.Reason
}
switch ($validationResult.Status) {
"Empty" { [void]$reportData.EmptyFiles.Add($fileInfo) }
"EmptyData" { [void]$reportData.EmptyDataFiles.Add($fileInfo) }
"InvalidFormat" { [void]$reportData.InvalidFormatFiles.Add($fileInfo) }
"Error" { [void]$reportData.ErrorFiles.Add($fileInfo) }
}
Write-ColorOutput " Skipped: $($file.Name) - $($validationResult.Reason)" -Type "Warning"
}
}
if ($validFiles.Count -eq 0) {
Write-ColorOutput "No valid hardware hash CSV files found." -Type "Error"
# Generate report even on failure
if ($GenerateReport) {
$reportPath = "$outputPath.report_$(Get-Date -Format 'yyyyMMdd_HHmmss').txt"
$reportContent = "Hardware Hash CSV Combine Report - FAILED`n"
$reportContent += "=" * 70 + "`n"
$reportContent += "No valid CSV files found to combine.`n`n"
$reportContent += "Empty Files (0 bytes): $($reportData.EmptyFiles.Count)`n"
$reportContent += "Empty Data Files (header only): $($reportData.EmptyDataFiles.Count)`n"
$reportContent += "Invalid Format Files: $($reportData.InvalidFormatFiles.Count)`n"
$reportContent += "Files with Errors: $($reportData.ErrorFiles.Count)`n"
$reportContent | Out-File -FilePath $reportPath -Encoding UTF8
Write-ColorOutput "Report saved to: $reportPath" -Type "Info"
}
exit 1
}
Write-ColorOutput "$($validFiles.Count) valid file(s) will be combined" -Type "Success"
# Create backup if output file exists
if ($CreateBackup -and (Test-Path $outputPath)) {
$backupPath = "$outputPath.backup_$(Get-Date -Format 'yyyyMMdd_HHmmss')"
Write-ColorOutput "Creating backup: $backupPath" -Type "Info"
Copy-Item -Path $outputPath -Destination $backupPath -Force
Write-ColorOutput "Backup created successfully" -Type "Success"
}
# Combine CSV files by explicitly cloning objects to avoid nesting
Write-ColorOutput "Combining CSV files..." -Type "Info"
$allData = [System.Collections.ArrayList]::new()
$headerProperties = $null
$processedCount = 0
$totalRecordCount = 0
foreach ($file in $validFiles) {
try {
Write-ColorOutput "Processing: $($file.Name)" -Type "Info"
# Read raw CSV text first to check for malformed headers
$rawLines = Get-Content -Path $file.FullName -Encoding UTF8
if ($rawLines.Count -lt 2) {
Write-ColorOutput " Skipping $($file.Name) - not enough data" -Type "Warning"
continue
}
# Check if CSV has quoted/malformed header
$needsRepair = $false
if ($rawLines[0] -match '^".*,.*"$' -or $rawLines[1] -match '^".*,.*,.*"$') {
Write-ColorOutput " Detected malformed CSV format - repairing..." -Type "Warning"
$needsRepair = $true
}
if ($needsRepair) {
# Repair the CSV by removing quotes around entire rows
$repairedLines = @()
foreach ($line in $rawLines) {
# Remove surrounding quotes if the entire line is quoted
if ($line -match '^"(.*)"$') {
$repairedLines += $matches[1]
} else {
$repairedLines += $line
}
}
# Write repaired CSV to temp file and import it
$tempFile = [System.IO.Path]::GetTempFileName()
$repairedLines | Set-Content -Path $tempFile -Encoding UTF8
$csvContent = @(Import-Csv -Path $tempFile -Encoding UTF8)
Remove-Item -Path $tempFile -Force
Write-ColorOutput " Repaired and imported $($csvContent.Count) records" -Type "Success"
} else {
# Import CSV normally
$csvContent = @(Import-Csv -Path $file.FullName -Encoding UTF8)
}
if ($csvContent.Count -eq 0) {
Write-ColorOutput " Skipping $($file.Name) - no data after import" -Type "Warning"
continue
}
Write-ColorOutput " DEBUG: Imported $($csvContent.Count) records from $($file.Name)" -Type "Info"
# First file: capture header property names
if ($null -eq $headerProperties) {
$headerProperties = $csvContent[0].PSObject.Properties.Name
Write-ColorOutput " DEBUG: Header properties count: $($headerProperties.Count)" -Type "Info"
Write-ColorOutput " DEBUG: Header properties: $($headerProperties -join ' | ')" -Type "Info"
}
# Add records directly to collection
foreach ($record in $csvContent) {
[void]$allData.Add($record)
}
Write-ColorOutput " DEBUG: Total records in collection now: $($allData.Count)" -Type "Info"
$totalRecordCount += $csvContent.Count
$processedCount++
# Track successful file processing for report
$successInfo = [PSCustomObject]@{
FileName = $file.Name
FilePath = $file.FullName
RecordCount = $csvContent.Count
FileSize = [math]::Round((Get-Item $file.FullName).Length / 1KB, 2)
}
[void]$reportData.SuccessfulFiles.Add($successInfo)
Write-ColorOutput " Added $($csvContent.Count) record(s) from $($file.Name)" -Type "Success"
}
catch {
Write-ColorOutput "Error processing $($file.Name): $_" -Type "Error"
# Track processing error for report
$errorInfo = [PSCustomObject]@{
FileName = $file.Name
FilePath = $file.FullName
Error = $_.Exception.Message
}
[void]$reportData.ProcessingErrors.Add($errorInfo)
# Continue processing other files
}
}
if ($totalRecordCount -eq 0) {
Write-ColorOutput "No data records found to combine." -Type "Error"
exit 1
}
Write-ColorOutput "Total records collected: $totalRecordCount" -Type "Info"
# Add/Update Group Tag to "Standard" for all records
Write-ColorOutput "Setting Group Tag to 'Standard' for all records..." -Type "Info"
$groupTagUpdated = 0
$groupTagAdded = 0
$standardHBGReplaced = 0
foreach ($record in $allData) {
$hasGroupTag = $null -ne ($record.PSObject.Properties.Name | Where-Object { $_ -eq "Group Tag" })
if ($hasGroupTag) {
# Check if it's "Standard-HBG" and replace it
if ($record.'Group Tag' -eq "Standard-HBG") {
$record.'Group Tag' = "Standard"
$standardHBGReplaced++
} elseif ($record.'Group Tag' -ne "Standard") {
$record.'Group Tag' = "Standard"
$groupTagUpdated++
}
} else {
# Add Group Tag property with value "Standard"
$record | Add-Member -MemberType NoteProperty -Name "Group Tag" -Value "Standard" -Force
$groupTagAdded++
}
}
if ($groupTagAdded -gt 0) {
Write-ColorOutput " Added 'Group Tag' column to $groupTagAdded record(s)" -Type "Success"
}
if ($groupTagUpdated -gt 0) {
Write-ColorOutput " Updated Group Tag to 'Standard' for $groupTagUpdated record(s)" -Type "Success"
}
if ($standardHBGReplaced -gt 0) {
Write-ColorOutput " Replaced 'Standard-HBG' with 'Standard' for $standardHBGReplaced record(s)" -Type "Success"
}
# Remove duplicates if requested
if ($RemoveDuplicates) {
Write-ColorOutput "Removing duplicate entries..." -Type "Info"
$originalCount = $allData.Count
# Find the serial number property
$serialNumberProperty = $headerProperties | Where-Object {
$_ -like "*Serial*Number*"
} | Select-Object -First 1
if ($serialNumberProperty) {
# Remove duplicates based on serial number
$uniqueData = [System.Collections.ArrayList]::new()
$seenSerials = @{}
foreach ($record in $allData) {
$serial = $record.$serialNumberProperty
if (-not $seenSerials.ContainsKey($serial)) {
[void]$uniqueData.Add($record)
$seenSerials[$serial] = $true
}
}
$allData = $uniqueData
$removedCount = $originalCount - $allData.Count
$totalRecordCount = $allData.Count
Write-ColorOutput "Removed $removedCount duplicate(s), $totalRecordCount unique record(s) remain" -Type "Success"
}
else {
Write-ColorOutput "Could not identify serial number column for duplicate removal" -Type "Warning"
}
}
# Export combined data to CSV file
Write-ColorOutput "Writing combined CSV file..." -Type "Info"
Write-ColorOutput "DEBUG: About to export $($allData.Count) total records" -Type "Info"
# Convert ArrayList to array for Export-Csv
$dataToExport = $allData.ToArray()
# Export using Export-Csv with proper formatting for Intune
$dataToExport | Export-Csv -Path $outputPath -NoTypeInformation -Encoding UTF8 -Force
# Verify the file was written correctly
Write-ColorOutput "DEBUG: Verifying output file..." -Type "Info"
$verifyData = @(Import-Csv -Path $outputPath -Encoding UTF8)
Write-ColorOutput "DEBUG: Output file contains $($verifyData.Count) data records" -Type "Info"
# Validate the output file has correct headers for Intune
if ($verifyData.Count -gt 0) {
$outputHeaders = $verifyData[0].PSObject.Properties.Name -join ','
Write-ColorOutput "DEBUG: Output file headers: $outputHeaders" -Type "Info"
}
# Verify output file was created successfully
if (Test-Path $outputPath) {
$outputSize = (Get-Item $outputPath).Length
Write-ColorOutput "=========================================" -Type "Info"
Write-ColorOutput "SUCCESS: Combined CSV file created!" -Type "Success"
Write-ColorOutput "Output File: $outputPath" -Type "Success"
Write-ColorOutput "Total Records: $totalRecordCount" -Type "Success"
Write-ColorOutput "File Size: $([math]::Round($outputSize / 1KB, 2)) KB" -Type "Success"
Write-ColorOutput "Files Combined: $processedCount" -Type "Success"
Write-ColorOutput "=========================================" -Type "Info"
# Generate detailed processing report
if ($GenerateReport) {
$reportPath = "$outputPath.report_$(Get-Date -Format 'yyyyMMdd_HHmmss').txt"
Write-ColorOutput "Generating processing report..." -Type "Info"
$reportContent = @"
================================================================================
Hardware Hash CSV Combine Report
================================================================================
Generated: $(Get-Date -Format "yyyy-MM-dd HH:mm:ss")
Script Version: 1.0
SUMMARY
========================================
Total CSV Files Found: $($csvFiles.Count)
Successfully Combined: $($reportData.SuccessfulFiles.Count)
Total Records in Output: $totalRecordCount
Output File: $outputPath
Output File Size: $([math]::Round($outputSize / 1KB, 2)) KB
PROCESSING STATISTICS
========================================
Valid Files Processed: $($reportData.SuccessfulFiles.Count)
Empty Files (0 bytes): $($reportData.EmptyFiles.Count)
Empty Data Files (header only): $($reportData.EmptyDataFiles.Count)
Invalid Format Files: $($reportData.InvalidFormatFiles.Count)
Files with Validation Errors: $($reportData.ErrorFiles.Count)
Files with Processing Errors: $($reportData.ProcessingErrors.Count)
"@
# Add successfully combined files section
if ($reportData.SuccessfulFiles.Count -gt 0) {
$reportContent += @"
SUCCESSFULLY COMBINED FILES ($($reportData.SuccessfulFiles.Count))
========================================
"@
foreach ($file in $reportData.SuccessfulFiles) {
$reportContent += "`n [OK] $($file.FileName)`n"
$reportContent += " Records: $($file.RecordCount)`n"
$reportContent += " Size: $($file.FileSize) KB`n"
$reportContent += " Path: $($file.FilePath)`n"
}
$reportContent += "`n"
}
# Add empty files section
if ($reportData.EmptyFiles.Count -gt 0) {
$reportContent += @"
EMPTY FILES - 0 BYTES ($($reportData.EmptyFiles.Count))
========================================
"@
foreach ($file in $reportData.EmptyFiles) {
$reportContent += "`n [SKIP] $($file.FileName)`n"
$reportContent += " Reason: $($file.Reason)`n"
$reportContent += " Path: $($file.FilePath)`n"
}
$reportContent += "`n"
}
# Add empty data files section
if ($reportData.EmptyDataFiles.Count -gt 0) {
$reportContent += @"
EMPTY DATA FILES - HEADER ONLY ($($reportData.EmptyDataFiles.Count))
========================================
"@
foreach ($file in $reportData.EmptyDataFiles) {
$reportContent += "`n [SKIP] $($file.FileName)`n"
$reportContent += " Reason: $($file.Reason)`n"
$reportContent += " Path: $($file.FilePath)`n"
}
$reportContent += "`n"
}
# Add invalid format files section
if ($reportData.InvalidFormatFiles.Count -gt 0) {
$reportContent += @"
INVALID FORMAT FILES ($($reportData.InvalidFormatFiles.Count))
========================================
"@
foreach ($file in $reportData.InvalidFormatFiles) {
$reportContent += "`n [SKIP] $($file.FileName)`n"
$reportContent += " Reason: $($file.Reason)`n"
$reportContent += " Path: $($file.FilePath)`n"
}
$reportContent += "`n"
}
# Add validation error files section
if ($reportData.ErrorFiles.Count -gt 0) {
$reportContent += @"
FILES WITH VALIDATION ERRORS ($($reportData.ErrorFiles.Count))
========================================
"@
foreach ($file in $reportData.ErrorFiles) {
$reportContent += "`n [SKIP] $($file.FileName)`n"
$reportContent += " Reason: $($file.Reason)`n"
$reportContent += " Path: $($file.FilePath)`n"
}
$reportContent += "`n"
}
# Add processing error files section
if ($reportData.ProcessingErrors.Count -gt 0) {
$reportContent += @"
FILES WITH PROCESSING ERRORS ($($reportData.ProcessingErrors.Count))
========================================
"@
foreach ($file in $reportData.ProcessingErrors) {
$reportContent += "`n [ERROR] $($file.FileName)`n"
$reportContent += " Error: $($file.Error)`n"
$reportContent += " Path: $($file.FilePath)`n"
}
$reportContent += "`n"
}
# Add duplicate removal section if applicable
if ($RemoveDuplicates) {
$reportContent += @"
DUPLICATE REMOVAL
========================================
Duplicate removal was enabled.
Original record count before deduplication: (see processing log)
Final unique record count: $($allData.Count)
"@
}
$reportContent += @"
================================================================================
End of Report
================================================================================
"@
# Save report to file
$reportContent | Out-File -FilePath $reportPath -Encoding UTF8
Write-ColorOutput "Report saved to: $reportPath" -Type "Success"
}
}
else {
throw "Output file was not created successfully"
}
}
catch {
Write-ColorOutput "=========================================" -Type "Error"
Write-ColorOutput "FATAL ERROR: $($_.Exception.Message)" -Type "Error"
Write-ColorOutput "Stack Trace: $($_.ScriptStackTrace)" -Type "Error"
Write-ColorOutput "=========================================" -Type "Error"
exit 1
}