Guy’s Scripting Ezine – 97 Net Time /setsntp

Contents for Ezine 97 – Net Time /setsntp

Introduction to 2006 Ezines

The New Year is a classic time for fresh initiatives, coupled with reflections on the previous 12 months.  As far as this ezine is concerned, the new initiative is a 10 part series specializing in the basis and basics of VBScript.  As far as looking to the past, I just want to say that this ezine will continue with the twin aims of providing working script examples, while giving general learning points to help you adapt my scripts to your environment.  Occasionally, I will feature CSVDE, LDIFDE or DS Services. 

The other reason to retain your free ezine subscription is that I send out complimentary ebook whenever I write a new ebook, or update an existing one.

 ♣

This Week’s Secret

About five years ago, when this site was just a few pages, I noticed that the Logon Script page had more hits than all the rest of the pages added together.  Realising the interest in VBScript for Windows, I developed a whole scripting section.  Two years ago, I started this ezine primarily as a way of giving those who bought my ebooks an after sales service.

From the first ezine, my specialty has been getting people started.  So for the next ten editions I am going to concentrate on the basics of VBScript.  I will be choosing real world examples for you to copy and paste, but my real objective is to explain how scripts are put together.  In particular, I want to explain which elements of the script you can and should change, and which elements are fixed terms have fixed names that you should learn, apply, but never change.

This Week’s Mission

This Week’s Mission is to configure your machine to synchronize with an internet time server.  Trust me, accurate time is important on a server.  As yet, no-one can explain to my satisfaction, why left to its own devices, my $1,000 computer regularly loses nearly a minute every week.  Especially when my 10 year old, $5, ‘Mickey Mouse’ watch keeps perfect time.

Theory that it’s helpful to know.

  1. Windows operating systems have a built-in ‘net time’ command. 
  2. SNTP is the simple network time protocol, not to be confused with SMTP. 
  3. There are a number of publicly available time servers on the internet that are kept accurate to the nearest nanosecond, for example, at NIST (National Institute of Standards and Technology).

Important Scripting Principle

If we look at the big picture, then we realize that almost all scripts mimic actions that you can perform manually.  The secret of success is to perfect the manual steps then convert them to lines of VBScript code.  In this instance we want to create a cmd session by: Clicking on the Start Button, typing: run, cmd.  From the black ‘dos box’, we now type, net time /? to check the switches.  Try: net time /querysntp.  I was surprised that Windows Server 2003 returned time.windows.com, 0x1, as its time server.  We want to change this to an internet time server such as: time-a.nist.gov.

The crucial command is: net time /setsntp:time-a.nist.gov

Pure Scripting Learning Points

We have established the need for a command shell, which will run our net time instructions.  Therefore, we create turn to our VBScript skills and create a shell object called objShell.  The instruction is: objShell=CreateObject("Wscript.Shell").  My learning point is that you can call this object zpv23K instead of objShell.  However, VBScript would not understand makemeobj("Wscript.Shell"), it has to be precisely, CreateObject("Wscript.Shell").  WScript is VBScript’s underlying program or host, more of that another time, suffice to say that here we specify a .Shell type of object.  In other scripts we will create .Network objects, which support different WScript properties or ‘handles’.

The focal point of our script is ObjShell.  We will utilize two of its methods, .Run to create a cmd session and .SendKeys to mimic keystrokes that we enter at the cmd prompt.  I also use the .Sleep method, principally to tell the script to wait, so that I can see what is happening.  Such scripts rarely work perfectly first time and I need to see if the instructions are going according to plan.

Note: Both .SendKeys and .Sleep are interesting and useful for learning or applying ‘sticking plaster’ to broken scripts, however, in production scripts seek alternative methods.

As you get to know me, you will realise that I have an addiction to WScript.echo messages.  Many readers say that I am obsessed with calling for WSH boxes to confirm that my code has produced action.  What I reply is that in production scripts, or if the messages annoy you, just ‘ Rem out the WScript.echo lines using a ‘ single quote.

Guy Recommends: The Free IP Address Tracker (IPAT) IP Tracker

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

Example 1: VBScript to Set the Time Server

In Example 1, we will employ SendKeys set the Server’s time server.  This method is more long-winded than Example 2, my reason for showing you this method is to demonstrate how scripts mimic normal GUI or command line actions.

Instructions for setting the Time Server

This script is designed for servers, but should work fine on an XP machine.

  1. Copy and paste the example script below into notepad or use a VBScript editor.

  2. One advantage of a good script editor such as OnScript is that you can see the line numbers, which helps when you have to troubleshoot error messages.

  3. Save the file with a .vbs extension, for example: TimSrv.vbs 

  4. Double click TimSrv.vbs, observe the cmd session and check the message box.

  

 ‘===============================================
‘ VBScript Source File — Created with XLnow OnScript
‘ TimSrv.VBS
‘ AUTHOR: Guy Thomas
‘ COMPANY: Computer Performance
‘ DATE: January 2006
‘ COMMENT: Script to set the time server

‘==============================================
Option Explicit
Dim objShell
Dim intShortSleep, intLongSleep
Dim strTimeSrv

Set objShell = CreateObject("WScript.Shell")

‘ Values set
‘ strTimeSrv = "uk.pool.ntp.org"
strTimeSrv = "time-a.nist.gov"
intShortSleep = 1500 ‘ 1000 = 1 second
intLongSleep = 5000

‘ Cmd prompt opened with .Run method
objShell.Run "cmd"
Wscript.Sleep intLongSleep

‘ Crucial command
‘ Check how .Sendkeys passes the instruction to the CMD prompt
objShell.SendKeys "net time /setsntp:" & strTimeSrv
objShell.SendKeys "{Enter}"
Wscript.Sleep intLongSleep

‘ SendKeys closes Cmd prompt
objShell.SendKeys "Exit"
Wscript.Sleep intShortSleep
objShell.SendKeys "{Enter}"

‘ Cosmetic command to confirm the script has completed
Wscript.Echo " Time Server is " & strTimeSrv
WScript.Quit

VBScript Tutorial – Learning Points

Note 1:  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 2:  The defining command is : Set objShell = CreateObject("WScript.Shell").  Here is where we create a shell object that we can use to execute the net time command.

Note 3:  Compare how Sendkeys passes instructions to the CMD prompt with how you may issue the net time command manually.

Note 4:  Observe how the carriage return command {Enter} is encased in curly brackets.

Challenges

  • Try adjusting the value of the intLongSleep variable.
  • Experiment by adding extra {ENTER} commands to introduce more line breaks in the CMD prompt.
  • Amend the WSCript.echo message, or even ‘Rem it out with an apostrophe at the beginning.
  • Research other internet time servers.

Guy Recommends: Tools4ever’s UMRAUMRA The User Management Resource Administrator

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 onUMRA.

Example 2  Set the Time Server using .Run

This script is more efficient than Example 1, moreover, it does not rely on the SendKeys method.  Once again, the WScript.echo command is merely to demonstrate that the script has actually executed.

  

‘===============================================
‘ VBScript Source File — Created with XLnow OnScript
‘ TimSrvRun.VBS
‘ AUTHOR: Guy Thomas
‘ COMPANY: Computer Performance
‘ DATE: April 2007
‘ COMMENT: Script to set the time server

‘==============================================
Option Explicit
Dim objShell, strTimeSrv
Set objShell = CreateObject("WScript.Shell")

‘ Values set
strTimeSrv = "uk.pool.ntp.org"
‘strTimeSrv = "time-a.nist.gov"

objShell.Run "net time /setsntp:" & strTimeSrv
‘ Cosmetic command to confirm the script has completed
Wscript.Echo " Time Server is " & strTimeSrv
WScript.Quit

Summary of Net time /setsntp

Our mission is to keep the server’s time synchronized with an external time source.  From a scripting point of view, we create a shell object, and then send instructions via the .Sendkeys method.

See more about VBScript

VBScripts  • Ezines  • WMI  • Logon Scripts  • Tool Kit

Ezine 26 SendKeys  • Ezine 34 Scriptomatic  • Ezine 47 .put  •Ezine 51 Sleep  • Ezine 52 OS

Ezine 77 Scriptomatic  • Ezine 84 AcctInfo  • Ezine 88 Progress bar  • Ezine 89 SendKeys

Ezine 97 Net Time  • Ezine 98 W32 Time  • Ezine 99 Time services  • Ezine 120 Sendkeys