# compareTimeStamps.ps1

# Set variables
$source="T:\DIRECTORY1"
$destination="\\FILESHERVER\DIRECTORY1"
$sampleSize=100
$logFile="\\FILESHERVER\SHERVER1\sampleTimeStampLog.txt"

function sampleTimeStamp{
param(
[string]$sourceDirectory=$source,
[string]$destinationDirectory=$destination,
[int]$sampleCount=$sampleSize,
[string]$timeStampLog=$logFile
)
$timeStampTimer=[System.Diagnostics.Stopwatch]::StartNew()

# Enable Remote to remote symlink following if it's not already set
$r2rEnabled=fsutil behavior query SymlinkEvaluation | select-string -Pattern "Remote to remote symbolic links are enabled."
if (!($r2rEnabled)){fsutil behavior set SymlinkEvaluation R2R:1;}

#$sourceFiles=Get-ChildItem $source -recurse | ? { !($_.PsIsContainer -and $_.FullName -notmatch 'archive' -and $_.FullName.Length -lt 260) } | get-random -count $sampleSize | % {$_.FullName}
$sourceFiles=Get-ChildItem $sourceDirectory -recurse|?{!$_.PSIsContainer -and $_.FullName.Length -lt 260}|get-random -count $sampleCount | % {$_.FullName}
$filesCount=$sourceFiles.Count
$rootCount=$sourceDirectory.Length
$commonDenominatingPaths=$sourceFiles|%{$_.substring($rootCount,$_.length-$rootCount)}
write-host $commonDenominatingPaths;
$destinationFiles=$commonDenominatingPaths | %{[string]$destinationDirectory+[string]$_;}
$badStamps=0;

if ($filesCount -gt 1){
"Checking a sample of $sampleSize for any time stamp inaccuracies..."
for ($i=0;$i -lt $sourceFiles.Length;$i++){
$sourceFile=$sourceFiles[$i];
$destinationFile=$destinationFiles[$i];
$sourceTimeStamp=(gi "$sourceFile").LastWriteTime;
$destinationTimeStamp=(gi "$destinationFile").LastWriteTime;
$dTimeIsNull=$destinationTimeStamp -eq $null
$sTimeIsNull=$sourceTimeStamp -eq $null
if ($dTimeIsNull -or $sTimeIsNull){
if ($sTimeIsNull){
$output+="`r`nUnable to obtain timestamp of $sourceFile";
}else{
$output+="`r`nUnable to obtain timestamp of $destinationFile";
}
$filesCount--;
}else{
if ($sourceTimeStamp -ne $destinationTimeStamp){
$output+="`r`n$destinationFile timestamp of $destinationTimeStamp DOES NOT MATCH its source $sourceFile timestamp of $sourceTimeStamp";
$badStamps++;
}
}
}
# Output
$timeStampElapsedHours=[math]::round($timeStampTimer.Elapsed.TotalHours,2);
$output="`r`n------------$((($filesCount-$badStamps)/$filesCount).tostring('P')) of the files in a sample of $filesCount are having accurate time stamps. Command took $timeStampElapsedHours hours.--------------`r`n"+$output;
}else{$output="`r`n------------Insufficient number of files to process.------------`r`n";}

write-host $output;
Add-Content $timeStampLog $output;
$timeStampTimer.Stop();
}

sampleTimeStamp;