PowerShell Module for Time Synchronization – Get current time in UTC from internet


This PowerShell Workflow is part of PowerShell module for Time Synchronization on Windows and Windows Server. To get all features you need all PowerShell Workflows from the Time Sync module.

There is no reason to make this script PowerShell Workflow but I decided to write it as Workflow because it is part of other Workflows (of course Advanced Function could be also part of any Workflow).

Possibilities

  • Get current date and time in UTC (Coordinated Universal Time) from the internet.
  • The date and time is obtained from public website over HTTP (TCP 80) so it is possible to use this workflow in environments without direct access to internet (for example over proxy).
  • It is PowerShell Workflow so this script could be for example used via Service Management Automation (SMA) on Microsoft Azure.

Code

Workflow Get-VDateTimeInternetUtc
{
    <#
    .SYNOPSIS
        Get current date and time for internet.

    .DESCRIPTION
        Developer
            Developer: Rudolf Vesely, http://rudolfvesely.com/
            Copyright (c) Rudolf Vesely. All rights reserved
            License: Free for private use only

            "V" is the first letter of the developer's surname. The letter is used to distingue Rudolf Vesely's cmdlets from the other cmdlets.

        Description
            Get current date and time for internet.

        Requirements
            Developed and tested using PowerShell 4.0.

    .EXAMPLE
        'Get using HTTP'
        Get-VDateTimeInternetUtc

    .INPUTS

    .OUTPUTS

    .LINK
        https://techstronghold.com/
    #>

    [CmdletBinding(
        HelpURI = 'https://techstronghold.com/',
        ConfirmImpact = 'Medium'
    )]

    # Configurations
    $ErrorActionPreference = 'Stop'
    $ProgressPreference = 'SilentlyContinue'

    $webRequest = Invoke-WebRequest -Uri 'http://nist.time.gov/actualtime.cgi?lzbc=siqm9b'

    $milliseconds = [int64](($webRequest.Content -replace '.*time="|" delay=".*') / 1000)

    # Return
    InlineScript
    {
        (New-Object -TypeName DateTime -ArgumentList (1970, 1, 1)).AddMilliseconds($Using:milliseconds)
    }
}

One response to “PowerShell Module for Time Synchronization – Get current time in UTC from internet”

Leave a Reply

Your email address will not be published. Required fields are marked *

Active Directory Advanced function AlwaysOn Availability Groups AlwaysOn Failover Cluster Instances Building Cloud Cloud Cluster Cmdlet Database Deployment Design DFS Domain Controller DSC Fabric Failover Clustering File Server Group Policy Hardware Profile Host Hyper-V Installation Library Library Asset Library Server Network Operations Manager Orchestrator PowerShell PowerShell User Group PowerShell Workflow Security Service Manager SQL Server Storage System Center Template Time Time Synchronization Tips Virtual Machine Virtual Machine Manager VM Network VM Template Windows Server 2012 R2