Synchronizing the Local System Time

Posted: May 1, 2010 in Logon Script with Shell
Tags:


Synchronizing the local system to a central time source allows you to perform enterprise-wide tasks simultaneously. The basic syntax to synchronize the local clock with a specified time source is as follows:

Net Time \\server/commands

Here, \\server is the name of the time source server to sync with. This parameter is only necessary when syncing with a specific server. If this parameter is omitted (Net Time), the system will search the local domain for a time source server. /commands are any of the following parameters:

  • /SET—Sets the local time to the time source server
  • /Y—Forces to sync the time with the server specified, regardless of whether the server is a time source server or not
  • /DOMAIN:domainname—Searches the specified domain for a time source server

The following script attempts to sync the local system time with the server named servername. If this fails, the domain will be searched for a time source to sync with. To execute this script, proceed as follows:

  1. Create a new directory to store all files included in this example.
  2. Select Start|Run and enter “scriptfile.bat.”

Here, scriptfile is the full path and file name of a script file that contains the following:

@Echo Off

CLS ; Clears the screen

Set TServer=ServerName

Echo Syncing the time with %TServer%…

Net Time \\%TServer% /set /yes

If %errorlevel% NEQ 0 CLS && Goto Domain

CLS && Echo Sync Successful

Goto End

:Domain

Echo Searching the local domain for a time-server…

Net Time /set /yes

If %errorlevel% EQU 0 CLS && Echo Sync Successful && Goto End

CLS && Echo Time Sync Error

:End

Here, tserver is a variable containing the name of the time source server; NEQ is the “not equal to” operator; and && allows you to run a second command after the first has completed.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s