VMware Cloud Community
johnlennon
Enthusiast
Enthusiast

How do I get the host time of all ESX servers connected to a VC?

I'm using a script previously posted by Luc on how to get the time from the ESX server, however I have some ESX servers that are behind a firewall and don't have direct access, but are accessible via VC. Is there a way to obtain the time of all hosts when connecting to VC instead of each single ESX host?

I also have my hosts configured for America/New_York time and the clock set to local time, not GMT. The time returned by the script is 4 hours ahead - is this a bug?

function getTime {

param($myHost)

Get-vc $myHost -user root -password myPassword | out-null

$svcRef = new-object VMware.Vim.ManagedObjectReference

$svcRef.Type = "ServiceInstance"

$svcRef.Value = "ServiceInstance"

$serviceInstance = get-view $svcRef

$datetime = $serviceInstance.ServerClock

Write-Host $myHost, $datetime

}

getTime("myhost1.domain.com")

getTime("myhost1.domain.com")

Tags (3)
0 Kudos
4 Replies
LucD
Leadership
Leadership

Some thoughts

1) As I already remarked in the SDK states "A client should not rely on this method to get the current time on the server; instead, the client should use the CurrentTime method."

To use the the method instead of the property you can change your function like this

function  getTime2 {
  param([string] $myHost)
  
  Connect-VIServer -Server $myHost -password myPassword | out-null
  $svcRef = new-object VMware.Vim.ManagedObjectReference
  $svcRef.Type = "ServiceInstance"
  $svcRef.Value = "ServiceInstance"
  $serviceInstance = get-view $svcRef

  $datetime = $serviceInstance.CurrentTime()
  Write-Host $myHost, $datetime
}

2) Time keeping on a Linux host is perhaps confusing.

In the file /etc/sysconfig/clock you can see if the HW clock uses UTC or not and also what timezone is used

In my case the file contains

ZONE=Europe/Paris
UTC=true
ARC=false

So, the HW clock uses UTC and my timezone is CEST.

The property, serverClock, and the method, CurrentTime, return the UTC time, irrespective of what timezone you have defined.

3) There is no way, afaik, of getting from the VC the time on all the ESX hosts that are under the VC.

Perhaps a good idea for a future VITK build.


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

halr9000
Commander
Commander

Guys, don't forget that this:

  $svcRef = new-object VMware.Vim.ManagedObjectReference
  $svcRef.Type = "ServiceInstance"
  $svcRef.Value = "ServiceInstance"
  $serviceInstance = get-view $svcRef

can be replaced by this:

$serviceInstance = get-view ServiceInstance

This works for all of the "singleton" MoRefs. From the help:

Populates VI .NET view objects from VI inventory objects (VIObject), managed object reference (MoRef) or their IDs. The format for ID is a <type>-<value> string but for some objects with fixed names like AlarmManager and ServiceInstance you can use only <type> for the ID parameter.






Author of the upcoming book: Managing VMware Infrastructure with PowerShell

Co-Host, PowerScripting Podcast (http://powerscripting.net)

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
johnlennon
Enthusiast
Enthusiast

I have UTC set to false too:

UTC=false
ARC=false

and even using $serviceInstance.CurrentTime() gives me the same wrong time of 4 hours ahead for all hosts. Notice that it's correct in VI (NTP config), console and inside VMs, so it sounds like a bug. FYI: this is also the same I get from servers I didn't setup (I have access to servers setup and configured by others at my company).

0 Kudos
LucD
Leadership
Leadership

You didn't list the timezone value but I suspect from your earlier entry that it is EDT.

And the current timezone offset in New York is UTC - 4 hours.

Which is in fact what you're seeing.

So I don't think it is a bug.


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

0 Kudos