Page 1 of 1

add to a ntp registry, set default WO deleting any other ntp

Posted: 18 Sep 2015 10:14
by julesverne
I asked this question in stackoverflow http://stackoverflow.com/questions/32555877/add-to-a-ntp-registry-and-set-default-without-deleting-any-other-ntp-servers and unfortunately no one has responded, so then I thought dostips has always helped in the past, perhaps someone knows.

I needed to add an IP into the list of internet time servers in the date and time applet (internet time tab) and set that as the default via command line script. After exhaustive research over honestly a number of years I finally came up with the correct command line. It took so long to find the answer because simply adding to the registry never made it appear in the applet. Below is the code.

Code: Select all

Reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers /v "1" /t REG_SZ /d "192.16.1.2" /f


However, there is one issue I face because of this. This deletes whatever timeserver that was currently "1". What can I do to prevent this from happening? Cause usually the first entry is time.windows.com which is not something I want to delete.

To give an example of what my current date and time has in the registry when I export to .reg file

Code: Select all

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers]
@="1"
"1"="time.windows.com"
"2"="time.nist.gov"
"3"="time-nw.nist.gov"
"4"="time-a.nist.gov"
"5"="time-b.nist.gov"

Re: add to a ntp registry, set default WO deleting any other

Posted: 18 Sep 2015 10:25
by Squashman

Code: Select all

for /F %%G in ('reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers') do set num=%%G
set /A num=num + 1
Reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers /v "%num%" /t REG_SZ /d "192.16.1.2" /f

Re: add to a ntp registry, set default WO deleting any other

Posted: 18 Sep 2015 11:22
by penpen
julesverne wrote:I needed to add an IP into the list of internet time servers in the date and time applet (internet time tab) and set that as the default via command line script. After exhaustive research over honestly a number of years I finally came up with the correct command line. It took so long to find the answer because simply adding to the registry never made it appear in the applet. Below is the code.

Code: Select all

Reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers /v "1" /t REG_SZ /d "192.16.1.2" /f
If i understand you right, then this is not the right command to do that.
You are just setting entry "1" to "192.16.1.2".
But you want to add a new (unused) value and set the default to it.
(@Sqashman: The output of the registry entries may be unsorted, so you should search the highest number of the filtered (numbers only) output.)


Code: Select all

@echo off
setlocal enableExtensions enableDelayedExpansion
:: retrieve first unused entry
set "num=0"
for /F %%G in ('reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers ^| findstr "^[^a-z] ^("') do if %%G GTR !num! set "num=%%G"
set /A num=num + 1

:: add new entry
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers /v "%num%" /t REG_SZ /d "192.16.1.2" /f

::set default value to the new entry
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers /ve /t REG_SZ /d "%num%" /f

endlocal
(Currently untested.)

penpen

Edit 1-3: Just realized, that using "sort" on numbers is 'suboptimal'... corrected the algorithm.

Re: add to a ntp registry, set default WO deleting any other

Posted: 18 Sep 2015 11:38
by Squashman
penpen wrote:(@Sqashman: The output of the registry entries may be unsorted, so you should sort the filtered (numbers only) output.)
penpen

Good thinking!

I did not want to risk testing changing the default on my computer at work so thanks for doing that code.

Re: add to a ntp registry, set default WO deleting any other

Posted: 18 Sep 2015 12:22
by julesverne
Thanks @penpen and @squashman. So.. unfortunately your script doesn't do what I need. The applet doesn't list the time server when you look at the list of ntp servers after running the bat. I'm pretty sure the key to the whole thing lies in the reg export I listed. From looking at this it appears that perhaps @="1" represents the number of the default and perhaps "1" can't or shouldn't be changed via the command line at least. Because when I run my command it works but like I said it deletes the time.windows.com entry. So simply adding a the new server to say "6" as I believe your script does and setting the default to 6 doesn't work in at least showing the result visually in the applet.

Windows Registry Editor Version 5.00

Code: Select all

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers]
@="1"
"1"="time.windows.com"
"2"="time.nist.gov"
"3"="time-nw.nist.gov"
"4"="time-a.nist.gov"
"5"="time-b.nist.gov"

Re: add to a ntp registry, set default WO deleting any other

Posted: 18 Sep 2015 12:46
by Squashman
Tested on Windows XP. Works fine.

Re: add to a ntp registry, set default WO deleting any other

Posted: 19 Sep 2015 15:20
by julesverne
Squashman wrote:Tested on Windows XP. Works fine.
Unfortunately, this will be for Win 7 computers and up. I did come up with this script based on penpen's script and it seems to work on my computer. Going to test this out on a few other computers next week.


Code: Select all

@echo off
setlocal enableExtensions enableDelayedExpansion

:: retrieve first current entry and first unused entry
set /a "num=0"

for /f "usebackq tokens=1,3 delims= " %%G in (`reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers`) do (
   if "%%G" GTR "!num!" (
      set /a "num=%%G"
      echo %%H
      if "!num!" EQU "1" (
         set "movedntpserver=%%H"
      )
   )
)
set /A num=num + 1

:: move current first entry to bottom of the list
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers /v "%num%" /t REG_SZ /d "!movedntpserver!" /f

:: move first entry to bottom of the list
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers /v "1" /t REG_SZ /d "192.168.1.2" /f

endlocal


To be honest, it seems like a lot of code for what seems like should/would/could be a one line reg add command.

Re: add to a ntp registry, set default WO deleting any other

Posted: 19 Sep 2015 15:55
by penpen
Squashman wrote:Tested on Windows XP. Works fine.
Same for Windows 8.1 (32 bit): Working fine.
(So it will probably work under all windows versions.)


julesverne wrote:So.. unfortunately your script doesn't do what I need. The applet doesn't list the time server when you look at the list of ntp servers after running the bat.
Sounds like you havent done one of these:
- this batch must be "Run as administrator"
- you have to close the dialog with the internet time widget before you run the batch and reopen it after
(to avoid updating the regsitry by pressing "OK", or "Abort")

If it is not like it sounds, then execute the batch under the command shell ("cmd.exe"):
What are the informations given by "reg"?

If "reg" claims to successfully have changed the registry, then lookup the registry entry ("regedit") to see if it really has changed (maybe an admin or virus scanner has sandboxed registry edits; press "F5" to update the view).


julesverne wrote:From looking at this it appears that perhaps @="1" represents the number of the default and perhaps "1" can't or shouldn't be changed via the command line at least.
It is designed to be changed this way.
(Checked this under: Windows XP, Windows Vista, Windows 7, Windows 8.1, and Windows 10.)


In addition:
1) The default value specifies the actually used ntp server:
You may select another ntp server from the (original list) and check the registry.
The default value changes according to the selected server.

2) You shouldn't change existing (hardcoded) registry entries created by the OS (if it is not explicitely allowed by MS).
If these values are referenced by other parts of windows, then these parts may fail.
(Appending the list is allowed, but manually added items may be overwritten by updates - actually i can't find the MS technet page that describes this... maybe i add it later.)


penpen

Re: add to a ntp registry, set default WO deleting any other

Posted: 19 Sep 2015 16:37
by julesverne
julesverne wrote:So.. unfortunately your script doesn't do what I need. The applet doesn't list the time server when you look at the list of ntp servers after running the bat.
penpen wrote:Sounds like you havent done one of these:
- this batch must be "Run as administrator"

Running as admin appears to be working. I will test this next week to be sure. Thanks @penpen you raise good points about editing default registry settings which is why I wanted to get other opinions on exactly how to do this safely.

Re: add to a ntp registry, set default WO deleting any other

Posted: 26 Sep 2015 05:22
by julesverne
So, I remembered now why I gave up on this before. This bat works if you double click on the bat file or run it using Administrator cmd window. However I plan on running this from an .hta file. And neither using .shellexecute "runas" option nor the cmd "runas" makes this work.

I have full Admin UAC. Here's a basic hta file code to run the file as admin that calls @penpen bat file. Doesn't work.

Code: Select all

<script language="vbscript" type="text/vbscript">
Sub Window_Onload
   Set ObjShell = CreateObject("Shell.Application")
   ObjShell.ShellExecute "C:\datetime.bat", "192.168.1.195", "", "", 1
End Sub
</script>

Re: add to a ntp registry, set default WO deleting any other

Posted: 26 Sep 2015 20:34
by Squashman
Did you try running cmd.exe directly and pass the batch file name to it.

Re: add to a ntp registry, set default WO deleting any other

Posted: 27 Sep 2015 18:34
by penpen
Squasman's method (1) should work:

Code: Select all

Z:\>test.hta


There are also other methods (UAC must be enabled for most of them):
(2) You could create a shortcut to "mshta.exe", add the hta file as a parameter:

Code: Select all

C:\Windows\System32\mshta.exe "C:\batch\hta\test.hta"
And then you should be able to choosing execute as admin when right-clicking it

(3) You could start the vbs line as admin:

Code: Select all

ObjShell.ShellExecute "C:\datetime.bat", "192.168.1.195", "", "runas", 1
This has the drawback, that you may have to type in the admin password multiple times

(4) You could create a jscript (or vbs) that starts the hta as admin and doubleclick it:

Code: Select all

// Test.hta.js
var objShell = new ActiveXObject("shell.application");
objShell.ShellExecute("mshta.exe", "Z:\\test.hta", "", "runas", 1);
WScript.Quit()


(5) Beside this you may want to add to the hta a detection on adminrights (Test.hta):

Code: Select all

<html>
<head>
   <hta:application id="HelloExample" border="thick" borderStyle="complex"/>
   <title>HTA - Hello World</title>
   <script language="vbscript" type="text/vbscript">
      Set WshShell = CreateObject("WScript.Shell")
      Set ObjShell = CreateObject("Shell.Application")
      Dim isAdmin

      isAdmin = WshShell.Run("%comspec% /E:ON /V:ON /C ""net session & exit /B !errorLevel!""", 0, true)
      If isAdmin = 0 Then
         isAdmin = True
      Else
         isAdmin = False
      End If

      Sub OnClickButton

         If isAdmin = True Then
            Rem admin
            objShell.ShellExecute "Z:\test.bat", "", "", "", 1
         Else
            Rem no admin
            MsgBox "You have insufficient rights."
Rem   or maybe      objShell.ShellExecute "Z:\test.bat", "", "", "runas", 1
         End If
      End Sub
   </script>
</head>
<body>
   <input type="button" id="TestQueryButton" class="button" value="Test Query" name="TestQueryButton"  onClick="OnClickButton" />
</body>
</html>


Every Method has its advantages and drawbacks: I would prefer the combination of (2) and (6) - you would have:
- no additional script file,
- a warning if not logged in as an admin, and
- the hta should work even if uac is disabled (if not using the "remed out" line).

Sidenote:
I'm not sure if using "%comspec% /E:ON /V:ON /C ""net session & exit /B !errorLevel!""" is the best to detect if logged in with admin rights (return code 0) or not (most probably 2). I only have tested this on Win8 32 bit - it may fail on other windows versions.
If someone knows a better way to do this (preferably a vbs/jscript function / member access that succeds only with admin rights) please let me know.


penpen

Edit: The script is not executed on loading the hta anymore.

Re: add to a ntp registry, set default WO deleting any other ntp

Posted: 16 Feb 2016 21:38
by julesverne
Hi all.. I thought I washed my hands of this but found out this isn't always working. First let me answer this below:

julesverne wrote:So, I remembered now why I gave up on this before. This bat works if you double click on the bat file or run it using Administrator cmd window.


I found an interesting way to do "double click" on a bat file via CMD by using explorer.exe. Example:

Code: Select all

explorer.exe "batfile.bat"
I stumbled across it during my testing. Been meaning to tell you guys about it as there really isn't much information on explorer.exe via cmdline. It's not an inherent part of cmd so I get why it's not in ss64 but maybe it should be as it has some cool features.

So that's how I got around my problem, which leads me to today. I've got a computer here that won't run bat files as admin. It's an administrator account. UAC is off. Tried creating a shortcut of the bat and clicking on "run as administrator" in the windows properties. Nothing.

And just to confirm with you that Squashman's code
Squashman wrote:@echo off
setlocal enableExtensions enableDelayedExpansion
:: retrieve first unused entry
set "num=0"
for /F %%G in ('reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers ^| findstr "^[^a-z] ^("') do if %%G GTR !num! set "num=%%G"
set /A num=num + 1

:: add new entry
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers /v "%num%" /t REG_SZ /d "192.16.1.2" /f

::set default value to the new entry
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers /ve /t REG_SZ /d "%num%" /f

endlocal

does work to a degree...and.. the server name IS now showing up in the Windows timedate.cpl applet which is farther than I had gotten before. However it always errors when you sync regardless of whether you attempt to sync via cmdline using

Code: Select all

w32tm resync
or via the applet. However, if I manually add in the server into the applet and click the sync button.. it syncs fine. So.. something is still amiss. And also, @penpen Thanks for the hta code, that will certainly come in hand...and just so you're aware I think my email notifications must be off because I never noticed those last 2 comments in this thread. :?

[SOLVED!] Re: add to a ntp registry, set default WO deleting any other ntp

Posted: 17 Feb 2016 16:24
by julesverne
Okay so.. I found http://forum.psquared.net/PrintTopic7643.aspx

Which has the following code:

Code: Select all

ECHO Add Time Service registry entries.
REG ADD HKLM\SYSTEM\ControlSet001\Services\List /v "123:UDP" /t REG_SZ /d "123:UDP:*:Enabled:NTP Time Service" /f
REG ADD HKLM\SYSTEM\CurrentControlSet\Services\W32Time /v "Start" /t REG_DWORD /d "00000384" /f
REG ADD HKLM\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpClient /v "Enabled" /t REG_DWORD /d "00000001" /f
REG ADD HKLM\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpClient /v "SpecialPollInterval" /t REG_DWORD /d "00000384" /f
REG ADD HKLM\SYSTEM\CurrentControlSet\Services\W32Time\Parameters /v "NtpServer" /t REG_SZ /d "ntp2d.mcc.ac.uk,0x9" /f
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers /v 1 /t REG_SZ /d ntp2d.mcc.ac.uk /f
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers /v 2 /t REG_SZ /d ntp.cis.strath.ac.uk /f
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers /v 3 /t REG_SZ /d time.windows.com /f
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers /v 4 /t REG_SZ /d time.nist.gov /f
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers /v 5 /t REG_SZ /d time-nw.nist.gov /f
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers /v 6 /t REG_SZ /d time-a.nist.gov /f
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers /v 7 /t REG_SZ /d time-b.nist.gov /f

ECHO Set Service Control For Windows Time Service to When Network Interface Active and restart service.
sc triggerinfo w32time start/networkon stop/networkoff
Net Start w32time


I obviously edited it by adding my server and I added

Code: Select all

w32tm /resync
at the bottom and it works! The windows internet time applet now shows my server as well as the default windows time servers and it correctly resyncs via cmd and the applet. However most of this code is out of my comfort zone and knowledge. So I don't understand the top at all. Also.. I wasn't really psyched that the script basically rewrites the default windows ntpserver list. So I combined what @squashman came up with and created this:

Code: Select all

set pmt=C:\Serverdata\
setlocal enabledelayedexpansion
for /f "usebackq delims=*" %%a in (`type %pmt%SRRServer.txt`) do (
   set server=%%a
)

set "num=0"
for /f %%G in ('reg query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers ^| findstr "^[^a-z] ^("') do if %%G GTR !num! set "num=%%G"
set /a num=num + 1

ECHO Add Time Service registry entries.
REG ADD HKLM\SYSTEM\ControlSet001\Services\List /v "123:UDP" /t REG_SZ /d "123:UDP:*:Enabled:NTP Time Service" /f
REG ADD HKLM\SYSTEM\CurrentControlSet\Services\W32Time /v "Start" /t REG_DWORD /d "300" /f
REG ADD HKLM\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpClient /v "Enabled" /t REG_DWORD /d "00000001" /f
REG ADD HKLM\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpClient /v "SpecialPollInterval" /t REG_DWORD /d "300" /f
REG ADD HKLM\SYSTEM\CurrentControlSet\Services\W32Time\Parameters /v "NtpServer" /t REG_SZ /d "%server%,0x9" /f

reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers /v "!num!" /t REG_SZ /d %server% /f

reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers /ve /t REG_SZ /d "!num!" /f

ECHO Set Service Control For Windows Time Service to When Network Interface Active and restart service.
sc triggerinfo w32time start/networkon stop/networkoff
Net Start w32time

w32tm /resync