# encoding: ascii
# api: powershell
# title: Monitor folders
# description: Compare files in multiple folders against a reference set to provide an early detection of Cryptolocker
# version: 0.1
# author: dfsdiag
# license: CC0
# x-poshcode-id: 6042
# x-archived: 2016-05-17T12:52:46
# x-published: 2016-10-08T10:46:00
#
#
# TXT File containing the server hostnames or IPs you want to monitor
$monitoredServers = gc "C:\ServersToMonitor.txt"
# Directory containing the folder structure you want to compare
$referenceDirectory = "C:\CLEAN\"

# Get a list of files including the relative folder structure
$monitoredFiles = gci -Recurse $referenceDirectory | ? { ! $_.PSIsContainer } | % { $($_.FullName).replace("$referenceDirectory","") }

# Iterate through the list of servers
foreach ($server in $monitoredServers) {
    Write-Host "Checking: $server" -ForegroundColor GREEN
    
    # Iterate through  all the files found in the reference directory
    foreach ($file in $monitoredFiles) {
        Write-Host "Checking: \\$server\$file against $referenceDirectory\$file"
        
        # Check if the file exists and if it's contents match the reference files
        if ((!(Test-Path "\\$server\$file")) -or (Compare-Object $(gc "\\$server\$file") $(gc $referenceDirectory\$file))) {
               Write-Host "FILES DO NOT MATCH" -ForegroundColor RED
        }
    }
}
