Listing remote Date and Time with PowerShell

Recently I needed to find the remote time of multiple windows servers on the network and compare these, I wrote a quick function that uses WMI to pull this information and return it in a DateTime object format:

image

The Code

function Get-Time {
	<#
		.SYNOPSIS
			Gets the time of a windows server

		.DESCRIPTION
			Uses WMI to get the time of a remote server

		.PARAMETER  ServerName
			The Server to get the date and time from

		.EXAMPLE
			PS C:\> Get-Time localhost

		.EXAMPLE
			PS C:\> Get-Time server01.domain.local -Credential (Get-Credential)

	#>
	[CmdletBinding()]
	param(
		[Parameter(Position=0, Mandatory=$true)]
		[ValidateNotNullOrEmpty()]
		[System.String]
		$ServerName,

		$Credential

	)
	try {
			If ($Credential) {
				$DT = Get-WmiObject -Class Win32_LocalTime -ComputerName $servername -Credential $Credential
			} Else {
				$DT = Get-WmiObject -Class Win32_LocalTime -ComputerName $servername
			}
	}
	catch {
		throw
	}

	$Times = New-Object PSObject -Property @{
		ServerName = $DT.__Server
		DateTime = (Get-Date -Day $DT.Day -Month $DT.Month -Year $DT.Year -Minute $DT.Minute -Hour $DT.Hour -Second $DT.Second)
	}
	$Times

}

#Example of using this function
$Servers = "localhost", "dc01.domain.local"

$Servers | Foreach {
	Get-Time $_
}

Checking for time skew

We can also use this function to easily check for time skew between two machines, the below code is an example where I check my time between a remote host and the local server to see if it is within 30 seconds…

$RemoteServerTime = Get-Time -ServerName "dc01.domain.local"
$LocalServerTime = Get-Time -ServerName "localhost"

$Skew = $LocalServerTime.DateTime - $RemoteServerTime.DateTime

# Check if the time is over 30 seconds
If (($Skew.TotalSeconds -gt 30) -or ($Skew.TotalSeconds -lt -30)){
	Write-Host "Time is not within 30 seconds"
} Else {
	Write-Host "Time checked ok"
}

6 thoughts on “Listing remote Date and Time with PowerShell

  1. Shakeel

    Hi

    When executing second script getting below errors. Please help

    Get-Time : The term ‘Get-Time’ is not recognized as the name of a cmdlet, function, script file, or operable program.
    Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At E:\compare-time.ps1:1 char:21
    + $RemoteServerTime = Get-Time -ServerName “192.168.100.100”
    + ~~~~~~~~
    + CategoryInfo : ObjectNotFound: (Get-Time:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

    Get-Time : The term ‘Get-Time’ is not recognized as the name of a cmdlet, function, script file, or operable program.
    Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At E:\compare-time.ps1:2 char:20
    + $LocalServerTime = Get-Time -ServerName “localhost”
    + ~~~~~~~~
    + CategoryInfo : ObjectNotFound: (Get-Time:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

  2. Kirill Pankin

    1) In fact, if we have a bad connection to remote host we can get diff time and can’t identify the reason – the connection or wrong settings. 2) if we need to specify credential we got the dialog to input password and it takes sec or two – the time will be different. I’m trying to find more accurate way to compare time

  3. Domran

    I’m trying to use this but can’t get the results to export and ideally email out the results in the body of the email formatted as html? please help?

  4. Tabi

    Just was I needed. Used to check time settings for all machines in a domain after an esx host was added to cluster with incorrect ntp settings
    Thanks.

  5. Pingback: VMTN Blog: Technical Marketing Update 2012 – Week 48 – #tmupdate | Virtualization

Leave a Reply to Tabi

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.