Set reliable time source (VBScript)

This code can be found in Chapter 3 of Active Directory Cookbook, 2nd edition

Purchase XP Cookbook or Networking Recipes for only $25 plus shipping! While supplies last.

Find out how to download all of the VBScript code from this site.

' Replace <PDCEmulatorName> with the DNS name of the PDC Emulator
' and <TimeServerNameOrIP> with the DNS name or IP address of the 
' reliable time source.

' ---------------------------------------------------------------
' From the book "Active Directory Cookbook" by Robbie Allen
' Publisher: O'Reilly and Associates
' ISBN: 0-596-00466-4
' Book web site: http://rallenhome.com/books/adcookbook/code.html
' ---------------------------------------------------------------

' ------ SCRIPT CONFIGURATION ------
strPDC = "<PDCEmulatorName>"            ' e.g. dc01.rallencorp.com
strTimeServer = "<TimeServerNameOrIP>"  ' e.g. ntp01.rallencorp.com
' ------ END CONFIGURATION ---------

strTimeServerReg = "SYSTEM\CurrentControlSet\Services\W32Time\Parameters"
const HKLM = &H80000002
set objReg = GetObject("winmgmts:\\" & strPDC & "\root\default:StdRegProv")
objReg.GetStringValue HKLM, strTimeServerReg, "ntpserver", strCurrentServer
WScript.Echo "Current Value: " & strCurrentServer
objReg.SetStringValue HKLM, strTimeServerReg, "ntpserver", strTimeServer
objReg.SetStringValue HKLM, strTimeServerReg, "type", "NTP"
strCurrentServer = ""
objReg.GetStringValue HKLM, strTimeServerReg, "ntpserver", strCurrentServer
WScript.Echo "New Value: " & strCurrentServer

' Restart Time Service
set objService = GetObject("winmgmts://" & strPDC & _
                           "/root/cimv2:Win32_Service='W32Time'")
WScript.Echo "Stopping " & objService.Name
objService.StopService()

Wscript.Sleep 2000  ' Sleep for 2 seconds to give service time to stop

WScript.Echo "Starting " & objService.Name
objService.StartService()

This code has been viewed 3238 times.

New from the creators of TechTasks.com: StatSheet.com