Kaseya Community

Script to monitor clock time

  • I'm trying to create a script that will check if the clock on an agent is correct. So far i have a script that will take the time of the agent and place it in a variable but now i need a way to compare that against the real time (ideally based on timezone but most of my agents are est.).

    Really all I'm missing is a way to import the real time to a .txt file. Once i do that i can compare the agent time vs. the real time. Any tips?

  • I found that it is easier to just correct the time then attempting to check if the time is correct or not. The way you can do this is to sync the end point's time with a ntp server.

    The reason why it can be difficult to do what you are asking for is because the time it takes to generate the time values for both the control and the endpoint could result in false positive that and putting in the ability to allow for time to be out by less then say 5min is complicated to script in Kaseya. If you really want to go down this path you would need to do it in a different language that can handle the complexities of dealing with time and then use a Kaseya procedure to execute and report on the out put.

  • Try this in a shell command:

    time /T >C:\temp\time.txt

    It should output the current time to the "C:\Temp" folder to a file called "time.txt"...

  • Hi Etabush,

    As zippo has explained you can "use time /t && date /t" to get the time and date of the local machine. Also use #vMachine.timezoneOffset# to get the timezone the machine is in encase its overseas. But to script and compare against Current VSA time will give false positives as HardKnoX has explained.

    It might be best for you to reference the fields in live connect under the Audit Information tab there is a Time Information section along the bottom.

    HardKnoX has posted about this before.


  • SMason-NTP.zip

    I've developed a couple scripts to handle all time syncing for Windows agents. You'll need to create a Custom Field called "Time Offset" if you want to see how far off clocks are in seconds. Time zones make no difference for this.

    You'll notice the first script "NTP: Fix Time" will check if we are dealing with a Domain Controller and adjust accordingly. All time is synced with ntp.pool.org as I believe this is the best source of time for our purposes.

    Anyway, I put a lot of time into this stuff (pun intended), but please enjoy freely. Let me know if there are questions.

    [edited by: SMason at 4:01 PM (GMT -7) on 5-15-2012] Correction.
  • HardKnoX, ideally the script would be able to only alert if off by more than x min to avoid false positives.

    Zippo, that command give the time from the agent's clock. I'm looking for the real time.

    Thinking the best way might be to just use the command zippo gave me and insert that into a custom column. Then schedule that procedure on a re-occurring interval and just manually monitor that column.

  • I guess I'm also curious about the point of this exercise, etabush. Do you want to know if there's a problem or just fix it if there is? It seems to me that HardKnoX is correct - if there's an issue then the best bet is just to go ahead and fix it. Looks like SMason has posted a great tool for doing so. I suppose you could add a function to email you if needed but Windows is pretty good about keeping time synced once it's set up correctly and I doubt if it would be much of an issue for you. You can run the "NET TIME" command on the clients to get them set and syncing correctly.


    [\\computername | /DOMAIN[:domainname] | /RTSDOMAIN[:domainname]] [/SET]

  • Ideally all our domain controllers should be synching with the ntp server but occasionally it wasnt setup right or wasnt setup at all. If this script shows me that any agents time is off then i will check the synch and see why its not working.

    Ya maybe its better to just use SMasons script which would fix the problem

  • @SMason - I just downloaded the scripts and they do a great job. Thanks.

  • No worries. Glad you like it.

  • Hi SMason!

    What is the scheduled script in the end of the NTP: Fix Time procedure?

    <Parameter xsi:type="StringParameter" name="ScriptName" value="1951531609" />

    I'm assuming it's NTP: Get Offset procedure.

    [edited by: neuvoja at 5:34 AM (GMT -7) on Oct 29, 2012] more info
  • Great scripts! Now how can I monitor this offset with an alarm? :)

  • Yes, the NTP: Get Offset runs after NTP: Fix Time. That is also where you can add steps to email alerts if you wish. Just note that it will try up to three times if it can't get a response (due to firewall or whatever).

  • I think I'll try event log alarms using time-service warnings.

  • Well, it works like a charm and I'm going to use it on all my DC's.