VMware Cloud Community
jengo00
Contributor
Contributor

Script to extract Time sync status for ESXi hosts

Hi,

Would like to know if there are any scripts to generate a time-sync report for the ESXi (4.0) hosts and VC in my Vcentre 4.0 infrastructure and send an email daily with the status ?

This is to ensure all the system time for the hosts are in sync. Am not good at scripting and some help would be appreciated ! Thanks

0 Kudos
7 Replies
tigerdeccan
Enthusiast
Enthusiast

This is to check all the hosts using right NTP

Get-VMHost |Sort Name|Select Name, @{N=“NTPServer“;E={$_ |Get-VMHostNtpServer}}, @{N=“ServiceRunning“;E={(Get-VmHostService -VMHost $_ |Where-Object {$_.key-eq “ntpd“}).Running}}

0 Kudos
tigerdeccan
Enthusiast
Enthusiast

if you do happen to have a host with an incorrect NTP server then you can set it like this:

Add-VMHostNtpServer -VMHost MYHost -NtpServer ‘ntp.mydomain.com‘

Or if one of the services has stopped you can start it again with the following:

Get-VmHostService -VMHost MyHost | Where-Object {$_.key -eq “ntpd“} | Start-VMHostService

0 Kudos
jengo00
Contributor
Contributor

Hi,

Thanks for your reply. However, this only checks if im connected to the right NTP Server and the service status. What I would require help is for me to be able to use a script to obtain the time stamp of all the ESXi hosts in a report format which i would in turn use to compare to my AD for accurate time sync. Thanks

0 Kudos
rldleblanc
Contributor
Contributor

This is crude, but this is what I'm using:

foreach($myHost in (Get-VMHost)) { $myView = Get-View $myHost.Extensiondata.ConfigManager.dateTimeSystem; Write-Host $(Get-Date) " " $myView.QueryDateTime() " " $myHost.Name; }

0 Kudos
Troy_Clavell
Immortal
Immortal

thread moved to VMware vSphere™ PowerCLI Community

0 Kudos
jengo00
Contributor
Contributor

Hi,

Thanks for your reply. I aint good at Powershell and as such would appreciate if you guide me into putting the sample Esxi host names into the script.

(Ex Server_Alpha, Server_Bravo, Server_Charlie)

Btw, do you have a sample output report of the script ?

Thanks so much.

Robert LeBLanc wrote:

This is crude, but this is what I'm using:

foreach($myHost in (Get-VMHost)) { $myView = Get-View $myHost.Extensiondata.ConfigManager.dateTimeSystem; Write-Host $(Get-Date) " " $myView.QueryDateTime() " " $myHost.Name; }

0 Kudos
LucD
Leadership
Leadership

You can use something like this

$esxNames = "Server_Alpha", "Server_Bravo", "Server_Charlie"
foreach($myHost in (Get-VMHost -Name $esxNames)) {     $myView = Get-View $myHost.Extensiondata.ConfigManager.dateTimeSystem     Write-Host $(Get-Date) " " $myView.QueryDateTime().ToLocalTime() " " $myHost.Name }

Note that I convert the system time to local time (it is returned in UTC).

For each server, the script will produce a line like this

29-03-2012 07:40:54   29-03-2012 05:40:32   Server_Alpha

There is a similar thread called PowerCLI: Detect incorrect Time Configuration


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos