Adding a server to Date and Time using Bat

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
julesverne
Posts: 81
Joined: 19 Nov 2013 00:41

Adding a server to Date and Time using Bat

#1 Post by julesverne » 22 Feb 2014 13:51

What I'd like to do: Add a server to Date and Time and then sync the computer to that server using .bat file.

I've tried to understand w32tm and net time commands looking at ss64 but I haven't figured out anything. Networking lingo is not fortay.

What I have found: topic here: https://groups.google.com/forum/#!topic ... pvUnYfxBkw

The topic is a description on how to do what I'm asking for except, it uses a deprecated cmd, because when i try

Code: Select all

net time /setsntp:time.nist.gov,0x1
i get

Code: Select all

The /QUERYSNTP and /SETSNTP options have been deprecated. Please use w32tm.ex
to configure the Windows Time Service.
. I'm using Windows 7 64 bit.

In the last thread of this topic I found the code below which I'm guessing just checks to see if the server is available, which of course would be useful but not the whole answer. I'm unable to test it here. Does anyone have access to a server to test?

Code: Select all

@echo off
setlocal enabledelayedexpansion
color 0a
for /f "tokens=1 delims= " %%a in ('net view^|findstr "\\"') do (
ping -n 1 %%~na|Find "TTL=" >nul && (call :checktime %%a) || echo %%~na *Offline*
)
goto :eof

:checktime
for /f "tokens=2 delims=:" %%c in ('w32tm /monitor /computers:%~n1^|find "NTP:"') do (
for /f "tokens=1" %%d in ("%%~nc") do (
set n=%%d
if !n:~-1! GTR 2 echo %1 :  %%c
)
)



Thanks for any help / suggestions!

Jules

npocmaka_
Posts: 517
Joined: 24 Jun 2013 17:10
Location: Bulgaria
Contact:

Re: Adding a server to Date and Time using Bat

#2 Post by npocmaka_ » 22 Feb 2014 14:43

Code: Select all

C:\>w32tm /monitor /computers:pool.ntp.org
pool.ntp.org[82.146.27.209:123]:
    ICMP: error IP_REQ_TIMED_OUT - no response in 1000ms
    NTP: -0.5161108s offset from local clock
        RefID: (unknown) [0x119446D4]
        Stratum: 3

Warning:
Reverse name resolution is best effort. It may not be
correct since RefID field in time packets differs across
NTP implementations and may not be using IP addresses.


But I cant handle the out put straightforward with the for loop/find so your checktime function does not work here...
A simple google search with "public ntp server" gives enough results.I think even Microsoft has one.

julesverne
Posts: 81
Joined: 19 Nov 2013 00:41

Re: Adding a server to Date and Time using Bat

#3 Post by julesverne » 22 Feb 2014 16:02

But I cant handle the out put straightforward with the for loop/find so your checktime function does not work here...
A simple google search with "public ntp server" gives enough results.I think even Microsoft has one.


pardon my ignorance @npocmaka_ but I'm very ignorant of networking, aside from ipconfig or ping. The code I provided was from that link I mentioned. I can ping ca.pool.ntp.org but that's all i can do. I don't know how to set it as a date and time server using bat or cmdline

Code: Select all

for /f "tokens=1 delims= " %%a in ('net view^|findstr "\\"') do (echo %%a)


looking at that for loop bit of code.. it doesn't do anything if i run it by itself. Because my impression is it's looking for something like.. \\pool.ntp.org which.. i don't think would work, cause I think my computer would have to be connected to pool.ntp.org

Again.. I am really ignorant as to what command whether it's net view or w32tm or other command that would be able to see in plain terms.. find a particular server... add the server to date and time automatically... and sync it.

julesverne
Posts: 81
Joined: 19 Nov 2013 00:41

Re: Adding a server to Date and Time using Bat

#4 Post by julesverne » 23 Feb 2014 23:59

So after a bit of exhausting google searches I've come across a solution (tested only my windows 7 64 bit machine), as well as a pretty great article on understanding w32tm more.

The really great article is here: http://blogs.technet.com/b/industry_insiders/archive/2006/08/29/w32-tm-service.aspx

The code listed here: http://www.computerperformance.co.uk/ezine/ezine98.htm is in vbs. However, it was easy enough to change to bat. Actually.. I think I prefer the bat version because he uses Wscript.Sleep and SendKeys. AutoIt script also takes advantage of SendKeys and sleep but even they say they can be very buggy. I love AutoIt, but I need something solid.

In addition I took out the sleep commands he had in the vbs because I believe they are more for vbs and how it handles cmdline commands and doesn't wait for them to finish. In a bat commands are executed consecutively unless of course use of functions and for commands, etc.

Code: Select all

@echo off
set serverIP=192.168.1.192
w32tm /config /syncfromflags:manual /manualpeerlist:%serverIP%
Net stop w32time >nul 2>&1
REM Added next line not in vbs code
Net start w32time >nul 2>&1
w32Tm /resync /rediscover
exit


The only curious message I get is

Code: Select all

The computer did not resync because only stale time data was available.
sometimes after I run the script repetitively. I've looked into the time and date manually by clicking on date and time and it does show however the message that it synced correctly to the IP. And..even more interesting is that message doesn't throw an errorlevel. So I guess more to the point that it isn't an error, just an unnerving message.

The final test (which I will try soon enough) is what happens after I restart the computer. Will it still know to sync to that IP? That server IP in the code isn't listed in date and time under time servers.. soo... I will get back to you on that.

julesverne
Posts: 81
Joined: 19 Nov 2013 00:41

Re: Adding a server to Date and Time using Bat

#5 Post by julesverne » 24 Feb 2014 00:26

The final test (which I will try soon enough) is what happens after I restart the computer. Will it still know to sync to that IP? That server IP in the code isn't listed in date and time under time servers.. soo... I will get back to you on that.


That is a no. It doesn't store that IP anywhere after restart. I'm not sure why I thought it would. So.. this is not a great solution for me.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Adding a server to Date and Time using Bat

#6 Post by foxidrive » 24 Feb 2014 00:33

What do you need to do exactly?

You can add NTP servers to in the time applet, right?

julesverne
Posts: 81
Joined: 19 Nov 2013 00:41

Re: Adding a server to Date and Time using Bat

#7 Post by julesverne » 24 Feb 2014 01:51

What do you need to do exactly? You can add NTP servers to in the time applet, right?


Not understanding time applet. Do you mean in Windows on the bottom right? If so, that's what I'm trying to do from cmd line or vbs or something, via some sort of script.

Post Reply