Get date remotely and convert it to different time zone

Today I thought I would share a one of my PowerShell function that I wrote for converting time zones from remote servers. If you have large scale environment you are probably dealing with several different time zones on your machines. To get date in different zone you can use below function.

Usage:

Get-DateInfo -TimeZone GMT
Get-DateInfo -Computername DC01 -TimeZone CST
Get-DateInfo -Computername DC01,DC02 -TimeZone GMT
Get-DateInfo -Computername (GC "C:\temp\servers.txt") -TimeZone EST

Output:

Get-DateInfo
Get-DateInfo

In this function I’ve just added most common time zones:

UTC – Coordinated Universal Time
IST – India Standard Time
GMT – GMT Standard Time
EST – Eastern Standard Time
CST – Central Standard Time

Of course you can use other and adjust the function. To get full list of available time zones you can just run one of the following commands:

[System.TimeZoneInfo]::GetSystemTimeZones() | Select ID,StandardName
[System.TimeZoneInfo]::GetSystemTimeZones() | Out-GridView
TimeZones
TimeZones

Final script:


Function Get-DateInfo {
    [CmdletBinding()]        
  
    # Parameters used in this function
    Param
    (
        [Parameter(Position=0, Mandatory = $false, HelpMessage="Please provide computer names", ValueFromPipeline = $true)] 
        $Computername = $env:COMPUTERNAME,

        [Parameter(Position=1, Mandatory = $true, HelpMessage="Select timezone(UTC, IST, GMT, EST, CST)", ValueFromPipeline = $true)][ValidateSet("UTC", "IST", "GMT", "EST", "CST")][string]
        $TimeZone
    )

    $Array = @()

    # TimeZones used in this function
    <#
    UTC                             Coordinated Universal Time 
    India Standard Time             India Standard Time
    GMT Standard Time               GMT Standard Time 
    Eastern Standard Time           Eastern Standard Time 
    Central Standard Time           Central Standard Time
    #>

    # Using switch for adding selected StandardName of timezone to $Val variable
    Switch ($TimeZone)
    {
        "UTC"     { $Val = "UTC" }
        "IST"     { $Val = "India Standard Time" }
        "GMT"     { $Val = "GMT Standard Time" }
        "EST"     { $Val = "Eastern Standard Time" }
        "CST"     { $Val = "Central Standard Time" }
    }

    If($Computername -ne $env:COMPUTERNAME)
    {
        # Looping each server
        Foreach($Comp in $Computername)
        {
            Try
            {
                $Array += Invoke-Command $Comp -ScriptBlock { param($Val,$TimeZone)

                    $LocalTime = [System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId([DateTime]::UtcNow,[System.TimeZoneInfo]::Local.Id)
                    $LocalTimeZone = [System.TimeZoneInfo]::Local.Id
                    $ConvertedTime = [System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId([DateTime]::UtcNow,"$Val")
                    New-Object PSObject -Property ([ordered]@{ 
                      
                        Server            = $env:COMPUTERNAME
                        LocalTime         = $LocalTime 
                        LocalTimeZone     = $LocalTimeZone
                        ConvertedTime     = $ConvertedTime 
                        ConvertedTimeZone = $TimeZone
                    })

                } -ArgumentList $Val,$TimeZone | Select-Object Server,LocalTime,LocalTimeZone,ConvertedTime,ConvertedTimeZone             
            }
            Catch
            {
                $_.Exception.Message
                Continue
            }
        }   
    }
    Else
    {
        Try
        {
            $Array += New-Object PSObject -Property ([ordered]@{ 
                      
                Computername      = $Computername
                LocalTime         = [DateTime]::Now.DateTime
                LocalTimeZone     = [System.TimeZoneInfo]::Local.Id
                ConvertedTime     = [System.TimeZoneInfo]::ConvertTimeBySystemTimeZoneId([DateTime]::UtcNow,"$Val")
                ConvertedTimeZone = $TimeZone
            })
        }
        Catch
        {
            $_.Exception.Message
            Continue
        }
    }

    # Display results in console
    If($Array)
    {
        Write-Host "`nResults:" -ForegroundColor Yellow
        $Array | Format-Table -AutoSize -Wrap
    }
}


Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.