How to Test NTP with PowerShell without Using W32Time?
Asked by
TechWanderer42
I'm working with servers that don't have the W32Time service running, and I need to verify if they're getting the correct time from an alternative solution, specifically DomainTime II from Greyware. I'm looking for a way to test the servers to ensure that firewalls and IP resolution are functioning correctly, but I can't find a PowerShell solution that doesn't involve using w32tm. I did find a C# example and managed to convert it into a native PowerShell function called Get-NTPTime. This function can either use an IP address you provide or look for the Windows Time NTP server in the registry. It creates a socket request to query the time server and returns the time in either local or UTC format. I hope someone finds this helpful!
Answers
This looks really interesting! Just a suggestion: consider adding a `-computername` parameter so you can directly use names like `pool.ntp.org` or specific IPs, just like `Test-NetConnection` and `Test-Connection`. You might need to adjust the handling of `[System.Net.IPEndPoint]` accordingly.
Reply to this answer
Consider setting a timeout to avoid infinite waits on bad addresses. Something like `$Socket.RetrieveTimeout = 5000` milliseconds could be helpful, especially if your firewall blocks requests.
Reply to this answer
I modified your function to work with `-computername` too! It now supports not just Windows but also macOS and Linux systems. Here's a sample call: `Get-NTPTime -ComputerName $(Get-NTPServer)`, and it successfully returned the current time for me!
Reply to this answer
While I haven't tried your code yet, I use w32tm for checking non-Windows NTP servers, including some mainframes and Linux systems. I'm curious why DomainTime II isn't reporting correctly unless w32tm is enabled, which isn't the case for all our servers. We stopped relying on third-party options after Microsoft improved w32time with Windows Server 2016.
Reply to this answer
I created a similar script using ChatGPT, which connects directly to an NTP server of my choice. Itโs a bit cleaner than restarting windows time services. We've found that if a remote PC's clock is significantly off, it can hinder remote session connections, but your script seems to be a good alternative for quickly syncing the time.
Reply to this answer
Add your answer
Answers are reviewed before publication.
Related Discussions