Check the time on multiple servers

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

?View Code 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


6 Comments

  1. Pete says:

    Hi I am getting these errors from the servers. Plus the Time is actually missing. It works great with just one server in the servers.txt.

    PS C:\> & ‘.\Check Time Zone.ps1’

    cmdlet Get-Credential at command pipeline position 1
    Supply values for the following parameters:
    Credential
    Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
    At C:\Check Time Zone.ps1:9 char:26
    + $timeObj = Get-WmiObject <<<< -ComputerName $servers -Class Win32_LocalTime -Credential $credential;
    + CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

    Oracletest set time is
    Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
    At C:\Check Time Zone.ps1:9 char:26
    + $timeObj = Get-WmiObject <<<< -ComputerName $servers -Class Win32_LocalTime -Credential $credential;
    + CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

    Oracletest1 set time is
    Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
    At C:\Check Time Zone.ps1:9 char:26
    + $timeObj = Get-WmiObject <<<

  2. Rhys says:

    Hi Pete,

    A typo got in the somehow. The line…

    $timeObj = Get-WmiObject -ComputerName $servers -Class Win32_LocalTime -Credential $credential;

    Should have been

    $timeObj = Get-WmiObject -ComputerName $server -Class Win32_LocalTime -Credential $credential;

    Cheers,

    Rhys

  3. heuristik says:

    still not working, getting same error as already reported. the issue seems to be in the way you are passing the env variable for $server?

    also, what about adding an initial list building query to get all the servers in a domain? something like:

    Import-Module ActiveDirectory

    $servers = Get-ADComputer -LDAPFilter “(operatingsystem=*Windows Server*)” | select name,dnshostname

    then you can bypass the step of reading a file from the desktop….

  4. Rhys says:

    I’ve checked this and it’s working fine for me. I’d remove the $env variable and put a fixed path in to see if that resolves anything.

    Cheers,

    Rhys

  5. Lawrence Knowlton says:

    I know this is an old thread, but I like the idea of the text file with a list of specified servers as not all of the servers may need to be checked. We check 5 servers using a command line batch file on XP, but NTP in Windows 7 doesn’t work the same in batch files. Shocking as it may seem, PowerShell seems to be the alternative. The example above might come in handy for us.

  6. Rhys says:

    Hi Lawrence,

    I generally prefer this to querying AD for servers as it give me a little more control.

    Rhys

Leave a Reply