Author Posts

January 1, 2012 at 12:00 am

by Ramrod at 2013-03-06 01:52:52

Hi there,

I'm trying to build a script, which syncs the windows system time with the internet. So far I found this:

function sync-time(
[string] $server = "nist1-ny.ustiming.org",
[int] $port = 37,
[switch] $set = $false)
{
$servertime = get-time -server $server -port $port -set:$set
write-host "Server time:" $servertime
write-host "Local time :" $(date)
}

Problem 1: The servertime remains empty
Problem 2: I don't know how to change my system time with this script

Thanks in advance for any kind of help

by Klaas at 2013-03-06 02:41:41

Did you write or import a Get-Time cmdlet? There doesn't seem to be one on my computer.
To sync the system clock and configure all sync settings, you can use w32tm.
w32tm /?

by Ramrod at 2013-03-06 02:54:15

[quote="Klaas"]Did you write or import a Get-Time cmdlet? There doesn't seem to be one on my computer.
To sync the system clock and configure all sync settings, you can use w32tm.
w32tm /?[/quote]

I don't know. The ISE gave me no error, while running the function. The w32tm seems useful, but I'm not good enough with Powershell to work out the whole
script by myself. Could you give me a hint ?

by Klaas at 2013-03-06 04:27:57

The point is I don't think Powershell will be that usefull. The settings for time synchronisation are in the Windows registry (HKLM\System\CurrentControlSet\Services\W32Time On Windows7). If you wish to change those settings, you use the w32tm. You can do that from a Powershell window or cmd.exe. I don't think it's a good idea to interfere with time sync via scripts or scheduled tasks.
with w32tm /query /configuration or w32tm /monitor you collect some usefull information and with w32tm /config you can alter the settings, but I suggest you only do that if you're aware of the dangers.

March 28, 2014 at 5:33 am

My solution was to actually use a batch for right now and just do the following and it worked just fine.

w32tm /resync

March 30, 2014 at 10:54 am

For best practice any domain member computer should sync time with the PDC Emulator FSMO role holder on the domain.

w32tm /config /syncfromflags:domhier /update

Restart-Service w32time

You should not sync with an outside NTP server unless your a stand alone server or computer in a workgroup. Or you happen to be on that server which holds that FSMO role.