After the recent change in our clocks due to BST I noticed that one of our servers was a hour slow. I wanted to check the rest since we run a lot of time dependant processes. Now we have Powershell any thought of manually checking each one is madness. Here’s a quick script I knocked up to check the time on multiple servers. Just create a text file called servers.txt with each server name on a new line. Place this onto your desktop and you’re ready to go.

Since my standard Windows Domain account didn’t have access to these servers I’ve used the Get-Credential cmdlet. When you execute the script you will see the below window asking for authentication details.

Get-Credential Powershell

# Get servers from text file on Desktop
$servers = Get-Content "$env:USERPROFILE\Desktop\servers.txt";
# Get credentials needed to access these servers
$credential = Get-Credential;

foreach($server in $servers)
{

	$timeObj = Get-WmiObject -ComputerName $server -Class Win32_LocalTime -Credential $credential;
	$hour = $timeObj.Hour;
	$minute = $timeObj.Minute;
	$second = $timeObj.Second;
	Write-Host "$server set time is $hour $minute $second";
}

The reported times will differ by a few seconds, due to latency, but it should be easy to spot any that are way out.

Powershell Server Time Check