Get-Ntptime.ps1
something exciting
Some information about the exciting thing
Table of contents generated with markdown-toc
Script
Function Get-NtpTime ( [String]$NTPServer ) {
# Build NTP request packet. We'll reuse this variable for the response packet
$NTPData = New-Object byte[] 48 # Array of 48 bytes set to zero
$NTPData[0] = 27 # Request header: 00 = No Leap Warning; 011 = Version 3; 011 = Client Mode; 00011011 = 27
# Open a connection to the NTP service
$Socket = New-Object Net.Sockets.Socket ( 'InterNetwork', 'Dgram', 'Udp' )
$Socket.SendTimeOut = 2000 # ms
$Socket.ReceiveTimeOut = 2000 # ms
$Socket.Connect( $NTPServer, 123 )
# Make the request
$Null = $Socket.Send( $NTPData )
$Null = $Socket.Receive( $NTPData )
# Clean up the connection
$Socket.Shutdown( 'Both' )
$Socket.Close()
# Extract relevant portion of first date in result (Number of seconds since "Start of Epoch")
$Seconds = [BitConverter]::ToUInt32( $NTPData[43..40], 0 )
# Add them to the "Start of Epoch", convert to local time zone, and return
( [datetime]'1/1/1900' ).AddSeconds( $Seconds ).ToLocalTime()
}
Download
Please feel free to copy parts of the script or if you would like to download the entire script, simple click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.
Report Issues
You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.