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 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.
















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 <<<
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
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….
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