当前位置: 代码迷 >> 综合 >> Windows ntp时间同步设置(bat脚本)
  详细解决方案

Windows ntp时间同步设置(bat脚本)

热度:25   发布时间:2024-03-08 08:02:15.0

来源:收录并测试通过

文件取名:Windows_NTP.bat

作用:通过修改注册表项来配置NTP时间同步地址

适合:windows2008--windows2016

脚本类型:.bat

脚本内容:

@echo off
color 0a
title Windows服务器NTP配置
:: 如果无设置DNS的,要在C:\Windows\System32\drivers\etc\hosts添加域名解析记录
:: 部分主机没有配置内网DNS,需要在内部DNS服务器添加NTP 服务器ntp.al8.cn.com 正反向解析。
:: 修正windows 2008 -windows 2012 R2系统默认windows time服务不自动启动问题
:: ######################################################
echo 【开始自动配置Windows服务器NTP地址为ntp.shenzhenair.com,请稍后.........】
echo=
@Rem 启用并重新配置NTP时间服务器为:ntp.ald8.cn
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers /f /v 0 /t REG_SZ /d "ntp.ald8.cn"
reg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers /f /t REG_SZ /d "0"
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\W32Time\Parameters" /v NtpServer /t REG_SZ /d ntp.shenzhenair.com,0x9 /f
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\W32Time\Parameters" /v Type /t REG_SZ /d NTP /f
echo 禁用本机对外提供NTP时间服务器。
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\W32Time\TimeProviders\NtpServer" /v Enabled /t REG_DWORD /d 0x00000000 /f
echo 启用本机NtpClient提供程序,以和外部NTP进行同步。
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\W32Time\TimeProviders\NtpClient" /v Enabled /t REG_DWORD /d 0x00000001 /f
echo 该设定宣布或授权本服务器不作为NTP对外时间服务器。
reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\W32Time\Config" /v AnnounceFlags /t REG_DWORD /d 0x00000000 /f
echo 设置本机和NTP服务器时间同步周期为10小时(36000秒)
reg add HKLM\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpClient /v SpecialPollInterval /t REG_DWORD /d 0x00008ca0 /f
:: 根据windows系统版本不同,开始做不同配置
for /f "tokens=4-5 delims=. " %%i in ('ver') do set VERSION=%%i.%%jif "%version%" == "6.0" (
echo.
echo 当前系统是:Windows Server 2008.
echo 删除默认w32time服务注册时默认触发器事件....
sc triggerinfo w32time delete
echo 设置w32time服务触发器为网络可用时就开启和关闭.......
sc triggerinfo w32time start/networkon stop/networkoff
echo 设置windows time服务为延迟启动启动.....
sc config w32time start= delayed-auto
echo.
) else if "%version%" == "6.1" (
echo.
echo 当前系统是:Windows Server 2008 R2. 
echo 删除默认w32time服务注册时默认触发器事件....
sc triggerinfo w32time delete
echo 设置w32time服务触发器为网络可用时就开启和关闭.......
sc triggerinfo w32time start/networkon stop/networkoff
echo 设置windows time服务为延迟启动启动.....
sc config w32time start= delayed-auto
echo.
) else if "%version%" == "6.2"  (
echo.
echo 当前系统是:Windows Server 2012.
echo 删除默认w32time服务注册时默认触发器事件....
sc triggerinfo w32time delete
echo 设置w32time服务触发器为网络可用时就开启和关闭.......
sc triggerinfo w32time start/networkon stop/networkoff
echo 设置windows time服务为延迟启动启动.....
sc config w32time start= delayed-auto
echo.) else if "%version%" == "6.3" (
echo.
echo 当前系统是:Windows Server 2012 R2. 
echo 删除默认w32time服务注册时默认触发器事件....
sc triggerinfo w32time delete
echo 设置w32time服务触发器为网络可用时就开启和关闭.......
sc triggerinfo w32time start/networkon stop/networkoff
echo 设置windows time服务为延迟启动启动.....
sc config w32time start= delayed-auto
echo.) else if "%version%" == "10.0" (
echo 当前系统是:Windows Server 2016. 
echo 设置windows time服务为延迟启动启动.....
sc config w32time start= delayed-auto)
echo.
@REM 重启NTP服务并同步
gpupdate /force
net stop w32Time
net start w32Time
w32tm /config /update
w32tm /resync 
echo 正在重启系统NTP服务和重新同步,请稍后.......
echo=
echo 当前系统NTP源地址:
w32tm /query /source 
echo=
echo=
echo 当前系统NTP服务运行情况:
w32tm /resync 
w32tm /query /status
w32tm /query /status /verbose
w32tm /query /peers
echo=
echo=
TYPE C:\WINDOWS\system32\drivers\etc\hosts
echo 【NTP服务配置完成,部分服务器可能需要重启系统后生效】
echo=
echo 删除源脚本文件
del /F /S /Q C:\Users\Administrator\Desktop\Windows_NTP.bat

 

 

  相关解决方案