STampa
STampa

Reputation: 341

Windows Server Time Incorrect After Updates/Reboot

We're running Windows Server 2012 R2. I've found that when the server installs updates and restarts (at 4am daily) it will restart in the incorrect time zone. Our time size is EST -5 (currently -4 with Daylight Savings), and the server defaults to UTC -0, which is 4 hours ahead of our time zone.

When we originally discovered the issue we found that the only workaround that actually worked was to use the task scheduler to run a sync command when the server restarted. That task and command worked well until two weeks ago, around 6/15/2017, and since then it hasn't worked. I've reviewed the task/event logs and it claims to have run successfully, yet it hasn't.

My only thought at this point is that there was something in the Windows update that rendered the sync code useless. But I'm at a loss for what that could be. Does anyone have suggestions about how to identify the problem?

The sync code we are using is in a .bat file. The two Windows updates installed on the morning of 6/14, which I suspect could be the cause of the task no longer working, are:

2017-06 Security Monthly Quality Rollup for Windows Server 2012 R2 for x64-based Systems (KB4022726)

Windows Malicious Software Removal Tool for Windows 8, 8.1, 10 and Windows Server 2012, 2012 R2, 2016 x64 Edition - June 2017 (KB890830)

REM *** Retry for up to 15 minutes (90 retries @ 10 seconds each)
set retryCount=0

:SyncStart
if %retryCount == 90 goto SyncEnd
set /A retryCount=retryCount + 1

REM *** Resync the system clock
net start w32time
w32tm /resync
if errorlevel 1 goto SyncDelay
if errorlevel 0 goto SyncEnd

:SyncDelay
REM *** If unsuccessful, delay 10 seconds, then retry
choice /n /t:y,10>nul
goto SyncStart

:SyncEnd

Upvotes: 0

Views: 1004

Answers (1)

Mofi
Mofi

Reputation: 49127

The batch file contains a syntax error in line:

if %retryCount == 90 goto SyncEnd

The value of an environment variables must be referenced with %VariableName% on using standard expansion and not just %VariableName like a FOR loop variable (%I on cmd respectively %%I in a batch file) or a batch file argument (%0,%1, ...), or with !VariableName! on using delayed expansion which is not needed here.

The code could be also optimized:

@echo off
REM *** Retry for up to 15 minutes (90 retries @ 10 seconds each)
set retryCount=0

:SyncStart
if %retryCount% == 90 goto :EOF
set /A retryCount+=1

REM *** Resync the system clock
%SystemRoot%\System32\net.exe start w32time
%SystemRoot%\System32\w32tm.exe /resync
if not errorlevel 1 goto :EOF

REM *** If unsuccessful, delay 10 seconds, then retry
%SystemRoot%\System32\timeout.exe 10 /nobreak
goto SyncStart

Well, the code can be optimized even more:

@echo off
REM *** Retry for up to 15 minutes (90 retries @ 10 seconds each)
set retryCount=0
%SystemRoot%\System32\net.exe start w32time
%SystemRoot%\System32\timeout.exe 5 /nobreak

:SyncTime
%SystemRoot%\System32\w32tm.exe /resync
if not errorlevel 1 goto :EOF

REM *** If unsuccessful, delay 10 seconds, then retry
set /A retryCount+=1
if not %retryCount% == 90 %SystemRoot%\System32\timeout.exe 10 /nobreak & goto SyncTime

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • echo /?
  • goto /?
  • if /?
  • net /?
  • net start /?
  • rem /?
  • set /?
  • timeout /?
  • w32tm /?

And read the Microsoft support article Testing for a Specific Error Level in Batch Files.

Upvotes: 1

Related Questions