PowerShell Check Root-of-Truth against Multiple Hosts

  # The source directory to check against others
$Source = "C:\Temp\"

# Target servers that should have identical directories & files
$Targets = @("RT1", "RT2")

# Define file extensions to exclude (add or remove as needed)
$ExcludeExtensions = @(".log", ".tmp", ".bak", ".cache", ".DS_Store")

# The foreach loop to check
foreach ($Target in $Targets) {
    Write-Host "`nChecking $Target..." -ForegroundColor Cyan
    $TargetPath = "\\$Target\C$\Temp\"
    
    # Get ALL items from source (excluding specified extensions)
    $SourceItems = Get-ChildItem $Source -Recurse -Force | 
        Where-Object { $_.Extension -notin $ExcludeExtensions } |
        ForEach-Object {
            $RelativePath = $_.FullName.Substring($Source.Length)
            [PSCustomObject]@{
                Path = $RelativePath
                IsFile = -not $_.PSIsContainer
                Hash = if (-not $_.PSIsContainer) { (Get-FileHash $_.FullName -Algorithm MD5).Hash } else { $null }
            }
        }
    
    # Get ALL items from target (excluding specified extensions)
    $TargetItems = Get-ChildItem $TargetPath -Recurse -Force -ErrorAction SilentlyContinue | 
        Where-Object { $_.Extension -notin $ExcludeExtensions } |
        ForEach-Object {
            $RelativePath = $_.FullName.Substring($TargetPath.Length)
            [PSCustomObject]@{
                Path = $RelativePath
                IsFile = -not $_.PSIsContainer
                Hash = if (-not $_.PSIsContainer) { (Get-FileHash $_.FullName -Algorithm MD5).Hash } else { $null }
            }
        }
    
    # Rest of your script remains the same...
    # Find missing items
    $Missing = $SourceItems | Where-Object { $_.Path -notin $TargetItems.Path }
    
    # Find extra items
    $Extra = $TargetItems | Where-Object { $_.Path -notin $SourceItems.Path }
    
    # Find files with different content
    $Different = @()
    foreach ($SourceFile in ($SourceItems | Where-Object { $_.IsFile })) {
        $TargetFile = $TargetItems | Where-Object { $_.Path -eq $SourceFile.Path -and $_.IsFile }
        if ($TargetFile -and $SourceFile.Hash -ne $TargetFile.Hash) {
            $Different += $SourceFile
        }
    }
    
    # Show results
    if ($Missing) {
        Write-Host "  ❌ MISSING on $Target :" -ForegroundColor Red
        $Missing | ForEach-Object { Write-Host "     $($_.Path)" }
    }
    
    if ($Extra) {
        Write-Host "  ➕ EXTRA on $Target :" -ForegroundColor Yellow
        $Extra | ForEach-Object { Write-Host "     $($_.Path)" }
    }
    
    if ($Different) {
        Write-Host "  🔄 DIFFERENT CONTENT on $Target :" -ForegroundColor Magenta
        $Different | ForEach-Object { Write-Host "     $($_.Path)" }
    }
    
    if (-not $Missing -and -not $Extra -and -not $Different) {
        Write-Host "  ✓ PERFECT MATCH - Content is identical!" -ForegroundColor Green
    }
} 

Leave a Reply

Your email address will not be published. Required fields are marked *