代码是在≥2012R2的系统上powershell测试的,代码未在≤2008R2的系统上测试;仅供非域环境参考。
腾讯云windows镜像2021年上半年优化过一次,其中有ntp相关的一些参数优化
针对老机器时间误差的问题,解决方案如下:
以管理员身份运行
首先执行reg query命令查看当前配置
reg query HKLMSYSTEMCurrentControlSetServicesW32TimeParameters | findstr "NtpServer Type"
reg query HKLMSOFTWAREPoliciesMicrosoftW32TimeParameters | findstr "NtpServer Type"
reg query HKLMSOFTWAREWOW6432NodePoliciesMicrosoftW32timeParameters | findstr "NtpServer Type"
reg query HKLMSYSTEMCurrentControlSetServicesW32TimeConfig | findstr "PollInterval"
reg query HKLMSYSTEMCurrentControlSetServicesW32TimeTimeProvidersNtpClient | findstr "PollInterval"
reg query HKLMSOFTWAREPoliciesMicrosoftW32TimeTimeProvidersNtpClient | findstr "PollInterval"
reg query HKLMSOFTWAREWow6432NodePoliciesMicrosoftW32TimeTimeProvidersNtpClient | findstr "PollInterval"
组策略NTP客户端配置里的Type要着重注意下
计算机配置 → 管理模板 → 系统 → Windows时间服务 → 时间提供程序 → 配置Windows NTP 客户端
NTP同步出错的问题,注意检查下这里是不是NTP类型,如果不是改成NTP类型,并重启windows time服务
公共镜像这里是NTP。
部分2008R2老机器如下图
然后执行下面命令优化(每隔1分钟校时一次)
net stop w32time 2>&1 > $null
#w32tm /unregister
#w32tm /register
sc.exe triggerinfo w32time delete ; sc.exe config w32time start= auto
sc.exe triggerinfo w32time start/networkon stop/networkoff
net start w32time
reg add "HKLMSYSTEMCurrentControlSetServicesW32TimeConfig" /v "MinPollInterval" /t reg_DWORD /d 5 /f
reg add "HKLMSYSTEMCurrentControlSetServicesW32TimeConfig" /v "MaxPollInterval" /t reg_DWORD /d 10 /f
reg add "HKLMSYSTEMCurrentControlSetServicesW32TimeTimeProvidersNtpClient" /v "SpecialPollInterval" /t reg_DWORD /d 60 /f
reg add "HKLMSOFTWAREPoliciesMicrosoftW32TimeTimeProvidersNtpClient" /v "SpecialPollInterval" /t reg_DWORD /d 60 /f
reg add "HKLMSOFTWAREWow6432NodePoliciesMicrosoftW32TimeTimeProvidersNtpClient" /v "SpecialPollInterval" /t reg_DWORD /d 60 /f
w32tm /config /update
w32tm /config /manualpeerlist:"time1.tencentyun.com,0x8 time2.tencentyun.com,0xa time3.tencentyun.com,0xa time4.tencentyun.com,0xa time5.tencentyun.com,0xa" /syncfromflags:manual /reliable:yes /update
net stop w32time
net start w32time
reg query HKLMSYSTEMCurrentControlSetServicesW32TimeParameters | findstr "NtpServer Type"
reg query HKLMSOFTWAREPoliciesMicrosoftW32TimeParameters | findstr "NtpServer Type"
reg query HKLMSOFTWAREWOW6432NodePoliciesMicrosoftW32timeParameters | findstr "NtpServer Type"
reg query HKLMSYSTEMCurrentControlSetServicesW32TimeConfig | findstr "PollInterval"
reg query HKLMSYSTEMCurrentControlSetServicesW32TimeTimeProvidersNtpClient | findstr "PollInterval"
reg query HKLMSOFTWAREPoliciesMicrosoftW32TimeTimeProvidersNtpClient | findstr "PollInterval"
reg query HKLMSOFTWAREWow6432NodePoliciesMicrosoftW32TimeTimeProvidersNtpClient | findstr "PollInterval"
w32tm /resync
start-sleep 3
w32tm /resync
start-sleep 3
w32tm /resync
SpecialPollInterval ∈ 【2^MinPollInterval,2^MaxPollInterval】
腾讯云平台目前的默认值是300 ∈ 【2^6,2^10】,我调成60 ∈ 【2^5,2^10】了,1分钟校时1次,过去的太乱了我半年前做了一次统计现在找不到了,印象中有3600(1小时),有604800(7天),乱得很,现在是300秒,阿里云也是300秒。
每5分钟校时一次的话命令如下
net stop w32time 2>&1 > $null
#w32tm /unregister
#w32tm /register
sc.exe triggerinfo w32time delete ; sc.exe config w32time start= auto
#sc.exe config w32time start= delayed-auto
sc.exe triggerinfo w32time start/networkon stop/networkoff
net start w32time
reg add "HKLMSYSTEMCurrentControlSetServicesW32TimeConfig" /v "MinPollInterval" /t reg_DWORD /d 6 /f
reg add "HKLMSYSTEMCurrentControlSetServicesW32TimeConfig" /v "MaxPollInterval" /t reg_DWORD /d 10 /f
reg add "HKLMSYSTEMCurrentControlSetServicesW32TimeTimeProvidersNtpClient" /v "SpecialPollInterval" /t reg_DWORD /d 300 /f
reg add "HKLMSOFTWAREPoliciesMicrosoftW32TimeTimeProvidersNtpClient" /v "SpecialPollInterval" /t reg_DWORD /d 300 /f
reg add "HKLMSOFTWAREWow6432NodePoliciesMicrosoftW32TimeTimeProvidersNtpClient" /v "SpecialPollInterval" /t reg_DWORD /d 300 /f
w32tm /config /update
w32tm /config /manualpeerlist:"time1.tencentyun.com,0x8 time2.tencentyun.com,0xa time3.tencentyun.com,0xa time4.tencentyun.com,0xa time5.tencentyun.com,0xa" /syncfromflags:manual /reliable:yes /update
net stop w32time 2>&1 > $null
net start w32time
reg query HKLMSYSTEMCurrentControlSetServicesW32TimeParameters | findstr "NtpServer Type"
reg query HKLMSOFTWAREPoliciesMicrosoftW32TimeParameters | findstr "NtpServer Type"
reg query HKLMSOFTWAREWOW6432NodePoliciesMicrosoftW32timeParameters | findstr "NtpServer Type"
reg query HKLMSYSTEMCurrentControlSetServicesW32TimeConfig | findstr "PollInterval"
reg query HKLMSYSTEMCurrentControlSetServicesW32TimeTimeProvidersNtpClient | findstr "PollInterval"
reg query HKLMSOFTWAREPoliciesMicrosoftW32TimeTimeProvidersNtpClient | findstr "PollInterval"
reg query HKLMSOFTWAREWow6432NodePoliciesMicrosoftW32TimeTimeProvidersNtpClient | findstr "PollInterval"
w32tm /resync
start-sleep 3
w32tm /resync
start-sleep 3
w32tm /resync
如果时间偏差超过15小时(54000秒)可能会无法自动校时,解决方案参考:https://cloud.tencent.com/developer/article/1920090