PowerShell Ezine, Logon Scripts

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.


If you are looking for handy network utilities, try some of the free downloads at Tools4Ever


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.

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.

Computer Training Software - Recommended Training VideosGuy Thomas recommends Computer Training Software

Their topics and material are ideal for getting you started with VBScript.  The videos are easy to follow and you can control the pace.  Try their free demo material and then see if you want to buy the full package. See more about VB Script Training CD.


 *


Google

Web  This website

Review of Orion NPMGuy Recommends: Orion's NPM - Network Performance Monitor

Orion's performance monitor is designed for detecting network outages. A network-centric view make it easy to see what's working, and what needs your attention.

This utility guides you through troubleshooting by indicating whether the root cause is faulty equipment or resource overload.

Download a free trial of the Network Performance Monitor

 

Home Copyright © 1999-2010 Computer Performance LTD All rights reserved

Please report a broken link, or an error.