Set Internet Time


posted by Tobias
04-06-2011

Downloads: 421
File size: 458 B
Views: 2,552

Embed
Set Internet Time
  1. function Get-InternetTime
  2.   $TcpClient = New-Object System.Net.Sockets.TcpClient 
  3.   [byte[]]$buffer = ,0 * 64 
  4.   $TcpClient.Connect('time.nist.gov', 13) 
  5.   $TcpStream = $TcpClient.GetStream() 
  6.   $length = $TcpStream.Read($buffer, 0, $buffer.Length); 
  7.   [void]$TcpClient.Close() 
  8.   $raw = [Text.Encoding]::ASCII.GetString($buffer
  9.   [DateTime]::ParseExact($raw.SubString(7,17), 'yy-MM-dd HH:mm:ss', $null).toLocalTime() 
  10.  
  11. Set-Date (Get-InternetTime
Filed under: , , , ,
provides a function called Get-InternetTime which retrieves the current internet time from a time server and converts the raw time information into a valid local datetime. It then uses this information to set the local date and time (this requires local admin privileges)
Copyright 2012 PowerShell.com. All rights reserved.