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)
    }
}
« Previous Next »

Part of the series

  1. PowerShell Module for Time Synchronization - PowerShell Workflows to get status, start, invoke, test, monitor and repair Time Sync
  2. PowerShell Module for Time Synchronization - PowerShell Workflow to get status and current configuration of Time Sync from remote servers in parallel
  3. PowerShell Module for Time Synchronization - PowerShell Workflow to start (invoke) Time Sync on remote servers in parallel
  4. PowerShell Module for Time Synchronization - PowerShell Workflow to wait for Time Sync and automatically correct (repair) Time Sync on remote servers in parallel
  5. PowerShell Module for Time Synchronization - PowerShell Workflow for testing and monitoring of Time Sync on remote servers in parallel