Condividi tramite


Testing Timestamps of Files Across a Slow Network

I’m not sure if this is because of network lag, or the fact that my lab is on a different AD domain (and different NTP clock) than my local computer.  All I know is:

 (Get-Item local\path\to\OriginalFile.txt).LastWriteTime –eq (Get-Item remote\path\to\CopyOfFile.txt).LastWriteTime

Does not work reliably.  My solution is to arbitrarily rule that if the timestamps are within 5 seconds, they’re good enough.  (Given that these files are manually updated, and I want to know if the files were recently updated or not, 5 seconds is fine.)

function Test-DateTimeWithinInterval 
{

     <#

.SYNOPSIS

Test if two timestamps are within the specified interval

.DESCRIPTION

Because file LastWriteTime DateTime values are stored to the millisecond, a copy of a file may not have the same value as the original. This allows for a "plus-or-minus" range, default of 5 seconds.

.PARAMETER ReferenceObject

First Datetime

.PARAMETER DifferenceObject

Second Datetime

.PARAMETER Interval

Timespan (measured in seconds) the two DateTime parameters must be within, absolute value, to return true.

#>

    param (

        [DateTime]$ReferenceObject,

         [DateTime]$DifferenceObject,

         [Int]$Interval = 5

     );

    [Math]::Abs(($ReferenceObject - $DifferenceObject).TotalSeconds) -le $Interval;

} # function Test-DateTimeWithinInterval

This is more of an exercise in using [Math]::Abs() than anything else.