Obtaining NTP Source and Delay (or drift)

Welcome Forums General PowerShell Q&A Obtaining NTP Source and Delay (or drift)

This topic contains 3 replies, has 3 voices, and was last updated by

 
Participant
3 months, 4 weeks ago.

  • Author
    Posts
  • #130211

    Participant
    Topics: 1
    Replies: 1
    Points: 70
    Rank: Member

    I'm looking for the most accurate way to query the NTP source and the amount of drift from that NTP source.

    Any advice or answers are welcome...

    I've been throwing this question around and I usually get one of two responses;
    1. I have no idea how to even begin something like that
    OR
          2. Ask someone else... I'm busy.

    I'd really appreciate any input on this subject.

  • #130364

    Participant
    Topics: 2
    Replies: 397
    Points: 689
    Helping Hand
    Rank: Major Contributor

    This looks to be a pretty thorough run through of one method.

    The code is a little outdated and needs some TLC, but it should be serviceable. 🙂

    Seeing as it has a prerequisite of a Windows service, any more direct solutions would probably require some P/Invoke shenanigans.

  • #130395

    Participant
    Topics: 12
    Replies: 215
    Points: 380
    Helping Hand
    Rank: Contributor

    Have used the following in a module.

    < #
    .Synopsis
       Helper function to perform the clock Drift test
    .DESCRIPTION
       Helper function to perform the clock Drift test
    .EXAMPLE
       Test-ClockDrift
    #>
    function Test-ClockDrift
    {
        # Get the clock drift by testing it aginst the google ntp service, one sample only.
        $timeDifference = w32tm.exe /stripchart /dataonly /computer:time.google.com /samples:1
    
        # The returned result is a string, split the string and get the time only.
        $clockDriftString = $timeDifference[3].Split(',')[1]
    
        # Remove any non-numeric characters.
        $clockDriftString = ($clockDriftString -Replace "[^0-9]{2}").Replace('s','').Replace(',','.')
    
        # Cast the string to decimal and round it to two decimals, this will also return the results.
        [Math]::Round(([decimal]$clockDriftString),2)
    }
    

    This will compare the current machines clock with the NTP source you state in the command line.
    Just modify it with whatever source (or parameterize it) you need etc.
    Accurate when it comes to NTP will always be a question on how accurate you need it to be 🙂

    E.g. the above will only grab one sample, to get a better result you probably want to grab multiple samples and do an average on those.
    When I wrote the above I only needed a ballpark figure just to see if it was completely out of wack (someone had reconfigured the PDC FSMO role on the wrong DC).

    • #131727

      Participant
      Topics: 1
      Replies: 1
      Points: 70
      Rank: Member

      Excellent Fredrik!

      This is exactly what I've been looking for.

      Thank you VERY much!!

The topic ‘Obtaining NTP Source and Delay (or drift)’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort