Untitled
unknown
plain_text
7 months ago
3.9 kB
5
Indexable
# Define arrays of source and destination folders (must be in same order)
$SourceFolders = @(
"\\ServerA\Folder1",
"\\ServerA\Folder2",
"\\ServerA\Folder3",
"\\ServerA\Folder4",
"\\ServerA\Folder5",
"\\ServerA\Folder6",
"\\ServerA\Folder7",
"\\ServerA\Folder8",
"\\ServerA\Folder9",
"\\ServerA\Folder10",
"\\ServerA\Folder11",
"\\ServerA\Folder12",
"\\ServerA\Folder13",
"\\ServerA\Folder14",
"\\ServerA\Folder15",
"\\ServerA\Folder16",
"\\ServerA\Folder17",
"\\ServerA\Folder18",
"\\ServerA\Folder19",
"\\ServerA\Folder20"
)
$DestinationFolders = @(
"\\ServerB\Folder1",
"\\ServerB\Folder2",
"\\ServerB\Folder3",
"\\ServerB\Folder4",
"\\ServerB\Folder5",
"\\ServerB\Folder6",
"\\ServerB\Folder7",
"\\ServerB\Folder8",
"\\ServerB\Folder9",
"\\ServerB\Folder10",
"\\ServerB\Folder11",
"\\ServerB\Folder12",
"\\ServerB\Folder13",
"\\ServerB\Folder14",
"\\ServerB\Folder15",
"\\ServerB\Folder16",
"\\ServerB\Folder17",
"\\ServerB\Folder18",
"\\ServerB\Folder19",
"\\ServerB\Folder20"
)
# Ensure both arrays have the same length
if ($SourceFolders.Count -ne $DestinationFolders.Count) {
Write-Error "The number of source and destination folders must be the same."
exit
}
$OverallDifferences = @()
# Function to compare file names and count
function Compare-FolderContent {
param(
[string]$SourcePath,
[string]$DestinationPath
)
$Differences = @()
# Get all file full paths and map to relative paths
$SourceFileMap = @{}
Get-ChildItem -Path $SourcePath -Recurse -File | ForEach-Object {
$relative = $_.FullName.Replace($SourcePath, "")
$SourceFileMap[$relative] = $_.FullName
}
$DestinationFileMap = @{}
Get-ChildItem -Path $DestinationPath -Recurse -File | ForEach-Object {
$relative = $_.FullName.Replace($DestinationPath, "")
$DestinationFileMap[$relative] = $_.FullName
}
$SourceFiles = $SourceFileMap.Keys
$DestinationFiles = $DestinationFileMap.Keys
$OnlyInSource = Compare-Object -ReferenceObject $SourceFiles -DifferenceObject $DestinationFiles -PassThru | Where-Object { $_ -in $SourceFiles }
$OnlyInDestination = Compare-Object -ReferenceObject $SourceFiles -DifferenceObject $DestinationFiles -PassThru | Where-Object { $_ -in $DestinationFiles }
if ($OnlyInSource.Count -gt 0) {
$Differences += "Files only in source:"
$Differences += $OnlyInSource | ForEach-Object { $SourceFileMap[$_] }
}
if ($OnlyInDestination.Count -gt 0) {
$Differences += "Files only in destination:"
$Differences += $OnlyInDestination | ForEach-Object { $DestinationFileMap[$_] }
}
return $Differences
}
# Loop through folder pairs
for ($i = 0; $i -lt $SourceFolders.Count; $i++) {
Write-Host "Comparing: $($SourceFolders[$i]) <-> $($DestinationFolders[$i])" -ForegroundColor Cyan
$Diffs = Compare-FolderContent -SourcePath $SourceFolders[$i] -DestinationPath $DestinationFolders[$i]
if ($Diffs.Count -eq 0) {
Write-Host "Folders are identical." -ForegroundColor Green
} else {
Write-Host "Differences found:" -ForegroundColor Red
$Diffs | ForEach-Object { Write-Host $_ }
$OverallDifferences += "==== $($SourceFolders[$i]) vs $($DestinationFolders[$i]) ===="
$OverallDifferences += $Diffs
}
}
# Final summary
if ($OverallDifferences.Count -eq 0) {
Write-Host "All folder pairs are identical." -ForegroundColor Green
} else {
Write-Host "Summary of differences:" -ForegroundColor Yellow
$OverallDifferences | ForEach-Object { Write-Host $_ }
}
Editor is loading...
Leave a Comment