Guy's Scripting Ezine 98 - W32tm
Contents for Ezine 98 - W32tm
One of my personal challenges is to try and practice what I preach - keep it simple, one step at a time. Last week I introduced 'net time' to synchronize a Domain Controller (DC) with a time server. In fact, we could have used W32tm instead. However, I wanted to start with a simple well-known command and only advance to the more flexible W32tm command, once I had reviewed the time synchronization concept.
This Week's Mission is to not only to configure the computers internet time server, but also to force our server to synchronize with that time server.
Before we get underway, I want to emphasise that in a domain, keeping accurate time is solely the responsibility of Windows Server 2003. XP workstations who are members of a domain need not worry, because they automatically synchronize with their DC. To be precise, XP computers synchronize with the Domain Controller holding the FSMO role called PDC emulator. I dare you to alter the time on an XP workstation by 10 minutes, then return 30 minutes later and see if my theory is correct. (This only works if the XP machine is in a domain.)
The W32tm command, which I feature this week, operates by passing command line instructions to the Windows Time service. Think of W32tm as a superior version of the net time command we used last week.
Both XP and Windows 200x server have the Windows Time service. I challenge you to scroll through the Services so that you can find the Time service, now you can see how to restart the Time service if your script fails.
In my overall scheme, this is week 2 of 10 in my introduction to VBScript course. As far as learning VBScript is concerned, W32tm and net time are only vehicles for us to experiment with VBScript. This week I am going to highlight the script's Header section and DIM statement.
Both the header section and the DIM statements fall into the category of, invest time now, which will repay handsomely when you troubleshoot a script. Before I go into detail, I want to repeat a recurring theme, VBScript provides numerous ways of writing lines of code, which achieve the same result. The significance of the last statement in this context is that both the header and DIM vary from author to author.
In my view the header region is where we should record the purpose of our script. I add a date and version number to keep track of my script; the first version always needs corrections and later versions are often improved as the result of user feedback. Incidentally, I like to add more comments before each major section of the script.
DIM or Dimension statements are optional. However, if you add the precise command: Option Explicit, then declaring variables becomes compulsory. The reason I always add Option Explicit after the header is that it prevents me misspelling variables later in the script. For example, if I start with intSleepLong = 3500 then call for a variable called intLongSleep later in the script, the result will not be what I expected. What will happen is that VBScript (via Option Explicit) will tell me that I did not declare intLongSleep, and this could save me half an hour of scratching my head while thinking, 'why isn't the delay working in this script?'
Guy Recommends: The Free IP Address Tracker (IPAT)
Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets. IPAT cracks this problem of allocating IP addresses in networks in two ways:
For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges.
For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker
The primary purpose of this example is to synchronize or resynchronize with a time server. Just in case no time server has been set, or you wish to swap time servers, the script includes a W32 time command to set the Time Server. This is the equivalent of the net time /setsntp we used in last week's script.
Sometimes the Windows Time service mysteriously stops, or appears started, yet is not working properly. Symptoms, when you run my script you get error 800706B5 or a similar code number. Fortunately, the solution is easy, navigate to the Administrative Tools, services and re-start the Windows Time Service.
I can hear you thinking, 'why doesn't Guy put a restart command in the script?' I could add the code, but remember my resolution to keep it simple? Restarting services will feature in future ezine, then you can bolt both together to solve the problem.
Instructions for synchronizing the Time Server
This script is designed for servers, but there is no reason why it should not work on an XP machine. If you use my time server (uk.pool.ntp.org or time-a.nist.gov), make sure that your machine has an internet connection.
Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.
It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information on UMRA.
VBScript Tutorial - Learning Points
Note 1: The header section, between the == explains the script's, purpose and records the date and version. This section is always optional, but recommended as it helps 6 months later when you need to amend the script.
Note 2: Option Explicit forces me to declare all my variables. This is why I have Dim statements at the top of my scripts, to dimension variables such as strTimeSrv.
Note 3: On line 14 this script creates the shell object called objShell. Later we employ objShell and the .SendKeys method to transmit keystrokes to the command prompt.
Note 4: W32tm is the command line equivalent of the Windows Time service that you see in the Services. Observe how the script sets the external time source with, config /syncfromflags:manual /manualpeerlist.
Note 5: This week's new feature is the w32Tm /resync /rediscover command, which was not available with 'Net time' in Ezine 97.
Summary of W32tm VBScript.
W32tm is a more versatile command than 'net time'. In particular you can now resync a computer with its time server.
From a VBScript point of view, get into the habit of adding or amending your script's header. Another best practice is to start your script proper with Option Explicit followed by the Dim statements.
See more about VBScript