我正在编写一个批处理文件,以在连接到我的服务器(域)的Windows中添加新的时间服务器,并将设备时间与服务器同步。
但是在我的设备中,有些服务器有2个时间服务器,而有些服务器有2个以上时间。因此,我无法在批处理文件中对值名称进行硬编码,因此我需要想出一种从注册表中获取值名称的方法(自动递增值并将其分配给值名称)。
到目前为止,我编写的代码如下:
@echo off
reg add HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows/CurrentVersion/DateTime/Servers /v 3 /t REG_SZ /d 13.127.xx.xxx
net stop w32time
w32tm /config /syncfromflags:manual /manualpeerlist:13.127.xx.xxx
net start w32time
w32tm /config /update
w32tm /resync /rediscover
@echo Time Sync Successful
pause
答案 0 :(得分:0)
由于您已经从batch-file中解析了注册表项,因此您最好使用它来执行大部分任务。 一旦您在行3
上更新了变量值,就应该:
@Echo Off
SetLocal EnableDelayedExpansion
Set "TSvr=13.127.xx.xxx"
Set "RKey=HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers"
Set "#=_=0"
Set "$="
For /F "EOL=HTokens=1-2*" %%A In ('"Reg Query "%RKey%" 2>Nul|Sort"')Do (
If "%%A"=="(Default)" (Set "DVal=%%C")Else If %%A Gtr !#! Set "#=%%A"
If "%%C"=="%TSvr%" Set "$=%%A")
If Defined $ (If "%DVal%"=="%$%" (Echo %TSvr% is already the default server.
GoTo End)Else (Echo %TSvr% is listed but not the currently the default.
Echo Setting it as the default entry...
Reg Add "%RKey%" /VE /D %$% /F>Nul 2>&1))Else (Set /A #+=1
Echo %TSvr% is not listed.
Echo Adding it and setting it as the default entry...
Reg Add "%RKey%" /V !#! /D "%TSvr%">Nul 2>&1
Reg Add "%RKey%" /VE /D !#! /F>Nul 2>&1)
SC Query state= inactive|Find "W32Time">Nul&&(Set "_=1"
SC Start W32Time>Nul 2>&1)
Echo Synchronising the time using %TSvr%.
W32Tm /ReSync>Nul
If %ErrorLevel%==0 (Echo %TSvr% was added and your time was synchronised to it.
)Else Echo %TSvr% was added but the time was not synchronised.
:End
Pause
If Defined _ SC Stop W32Time>Nul 2>&1
EndLocal
GoTo :EOF