Highlighted
Contributor
Contributor

PowerCLI: Is it possible for PowerCLI to change host time/clock?

Jump to solution

I am a fresher & looking for PowerCLI code to change host time/clock. I don't know is it possible or not. If yes, I would like to know how, thanks a lot!

0 Kudos
1 Solution

Accepted Solutions
Highlighted
User Moderator
User Moderator

You are right that there is currently no PowerCLI cmdlet available to manipulate the date/time of a host.

But from PowerCLI you have access to all the SDK methods and properties.

I assume you want to use the UpdateDateTime method ?

That is easily done from with a PowerCLI script.

Something like this.

$esx = Get-VMHost -Name MyEsxiServerName

$dtSystem = Get-View $esx.Extensiondata.ConfigManager.dateTimeSystem

$dtSystem.UpdateDateTime(Get-Date)


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

View solution in original post

0 Kudos
17 Replies
Highlighted
User Moderator
User Moderator

You should use NTP to set the host clock.

See the post Powershell – Configure NTP server list for an overview of the PowerCLI cmdlets you can use to set the NTP service.

The option to use the hwclock command via the COS should not be used anymore.


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

0 Kudos
Highlighted
Contributor
Contributor

Dear Lucd,

Thanks for the quick answer, the reason why we want to try this because our system don't have access to internet, can't use any public or external NTP server. For more information, please refer to my other question:

ESXi 4.1 NTP client fails to synchronize with local (WIN2K8 DC or WIN2K8)  NTP server

Thanks again & looking forward to your feedback again!

0 Kudos
Highlighted
User Moderator
User Moderator

You will have to configure the NTP service on the W2K8 DC. By default it will be W32Time which is not NTP.

See MS KB816042 on how to do this.

Once you have the NTP server service running on the DC, you can use the PowerCLI cmdlets from the previous post to configure the NTP client service on the ESX(i) server(s).

Make sure you configure the firewalls (Windows and ESX(i)) to pass the NTP protocol.

Note that this is not an ideal situation, I would advise strongly to use an external source as the NTP server(s) for the the ESX(i) servers.

But if there is no external source you can use, then the above is a usable alternative.


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

0 Kudos
Highlighted
Immortal
Immortal

If you can't have network access to global NTP servers via the Internet you should consider a GPS time server. Time on a physical or virtual machine can vary a great deal. If you have any regulatory or compliance requirements, accurate time will be very import. Constantly adjusting time will leave gaps or overlap in logs and events which won't make auditors happy.

-- David -- VMware Communities Moderator
0 Kudos
Highlighted
Contributor
Contributor

Thanks LucD,

I think I had setup windows 2k8 DC properly & also disabled the firewall. Follwoing is what I got from ESXi, if I don't have any external time source (it is actually what we have, if we have external/internet NTP server access then the problem solved, please refer to following message), then looks like the ESXi client don't send out the exact Stratum level & Reference-ID, please see the yellow text. And the sync doesn't work. Here the 10.xxx.xxx.39 is the physical server  windows 2K8 which sync to windows 2K8 DC 10.xxx.xxx.22.

00:24:02.484891 IP (tos 0x0, ttl 64, id 24350, offset 0, flags [none], proto UDP (17), length 76)
    10.xxx.xxx.24.123 > 10.xxx.xxx.39.123: NTPv4, length 48
        Client, Leap indicator: clock unsynchronized (192), Stratum 0, poll 6s, precision -20
        Root Delay: 0.000000, Root dispersion: 0.025985, Reference-ID: (unspec)
          Reference Timestamp:  0.000000000
          Originator Timestamp: 3516913319.850800599 (2011/06/13 00:21:59)
          Receive Timestamp:    3516913376.485506817 (2011/06/13 00:22:56)
          Transmit Timestamp:   3516913442.484852618 (2011/06/13 00:24:02)
            Originator - Receive Timestamp:  +56.634706199
            Originator - Transmit Timestamp: +122.634052038


00:24:02.485889 IP (tos 0x0, ttl 128, id 26645, offset 0, flags [none], proto UDP (17), length 76)
    10.xxx.xxx.39.123 > 10.xxx.xxx.24.123: NTPv3, length 48
        Server, Leap indicator:  (0), Stratum 2, poll 6s, precision -6
        Root Delay: 0.031250, Root dispersion: 10.086456, Reference-ID: 10.xxx.xxx.22
          Reference Timestamp:  3516913360.019334999 (2011/06/13 00:22:40)
          Originator Timestamp: 3516913442.484852618 (2011/06/13 00:24:02)
          Receive Timestamp:    3516913385.852334999 (2011/06/13 00:23:05)
          Transmit Timestamp:   3516913385.852334999 (2011/06/13 00:23:05)
            Originator - Receive Timestamp:  -56.632517635
            Originator - Transmit Timestamp: -56.632517635

If I put 10.xxx.xxx.39, the physical server  windows 2K8 sync to 1.pool.ntp.org,0x1 or time.windows.com,0x1 with temp internet access for sample system. ESXi can sync to 10.xxx.xxx.39. So it drive me crazy for about one week to find out what is the problem. :smileyconfused:

23:03:36.266755 IP (tos 0x0, ttl 64, id 49932, offset 0, flags [none], proto UDP (17), length 76)

    10.xxx.xxx.24.123 > 10.xxx.xxx.39.123: NTPv4, length 48

        Client, Leap indicator:  (0), Stratum 4, poll 6s, precision -20

        Root Delay: 0.165557, Root dispersion: 0.161331, Reference-ID: 10.xxx.xxx.39

          Reference Timestamp:  3516908488.262913794 (2011/06/12 23:01:28)

          Originator Timestamp: 3516908551.304229699 (2011/06/12 23:02:31)

          Receive Timestamp:    3516908551.264693798 (2011/06/12 23:02:31)

          Transmit Timestamp:   3516908616.266718807 (2011/06/12 23:03:36)

            Originator - Receive Timestamp:  -0.039535902

            Originator - Transmit Timestamp: +64.962489128

23:03:36.267592 IP (tos 0x0, ttl 128, id 17217, offset 0, flags [none], proto UDP (17), length 76)

    10.xxx.xxx.39.123 > 10.xxx.xxx.24.123: NTPv3, length 48

        Server, Leap indicator:  (0), Stratum 3, poll 6s, precision -6

        Root Delay: 0.164520, Root dispersion: 0.087005, Reference-ID: 64.4.10.44

          Reference Timestamp:  3516908536.423228499 (2011/06/12 23:02:16)

          Originator Timestamp: 3516908616.266718807 (2011/06/12 23:03:36)

          Receive Timestamp:    3516908616.311228499 (2011/06/12 23:03:36)

          Transmit Timestamp:   3516908616.311228499 (2011/06/12 23:03:36)

            Originator - Receive Timestamp:  +0.044509693

            Originator - Transmit Timestamp: +0.044509693

Thanks again!

0 Kudos
Highlighted
Contributor
Contributor

Hi DSTAVERT,

Actually the windows 2k8 DC has communicate to third party server to get the time, and we manually adjust it because the third party server sync to higher server in the system. That is why we don't have any internet access & don't think about GPS clock since we need to keep step with other system.

Thanks!

0 Kudos
Highlighted
User Moderator
User Moderator

Perhaps a stupid question, but did you restart the NTP service on the ESXi server after you switched the NTP server ?


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

0 Kudos
Highlighted
Contributor
Contributor

Yes, I did.

0 Kudos
Highlighted
Contributor
Contributor

Hi LucD,

I am a Linux fresher, could you please give me more comment about "hwclock command" & "COS", are they PowerCLI command which I can used to change ESXi timer? Thanks! 

0 Kudos
Highlighted
User Moderator
User Moderator

Of course, the COS (Console Operating System) is the console that is available with ESX.

It is a Red Hat host that contains multitple command to interface with ESX hypervisor.

On ESXi the COS is not available anymore.

There is a DCUI (Direct Console User Interface) that has a restricted set of commands available.

The DCUI is in fact an implementation of BusyBox.

See David's execellent post on the DCUI, called Understanding the VMware ESXi Direct Console User Interface (DCUI)

With some tweaking you can still use a SSH connection to this DCUI.

The hwclock command is a Linux command that allows you to display and set the HW clock of a system.

This will show the system time when executed in the COS

hwclock

and this will set the system time to the time on the HW clock.

hwclock -systohc


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

0 Kudos
Highlighted
Contributor
Contributor

Hi LucD,

Thank you very much for your kindly help. Now my understanding is if we prefer to change host time/clock on ESXi by scripts rather than GPS NTP timer server, the PowerCLI can't manage to do it but vSphere SDK for Perl does (I found "Managed Object - HostDateTimeSystem" in vSphere API Reference by follow your link Understanding the VMware ESXi Direct Console User Interface (DCUI), but I can't find any command about write host time within PowerCLI). Am I right?

If so, maybe I will focus on vSphere SDK for Perl. If vSphere SDK for Perl also can't handle this issue, then maybe we need to move on to GPS NTP timer server. But it increase the hardware cost since we have quite few different sites. And why we want to sync ESXi host just prevent the big time different then trig problem when we reboot any one of the virtual machines.

Thanks again!

0 Kudos
Highlighted
User Moderator
User Moderator

You are right that there is currently no PowerCLI cmdlet available to manipulate the date/time of a host.

But from PowerCLI you have access to all the SDK methods and properties.

I assume you want to use the UpdateDateTime method ?

That is easily done from with a PowerCLI script.

Something like this.

$esx = Get-VMHost -Name MyEsxiServerName

$dtSystem = Get-View $esx.Extensiondata.ConfigManager.dateTimeSystem

$dtSystem.UpdateDateTime(Get-Date)


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

View solution in original post

0 Kudos
Highlighted
Immortal
Immortal
And why we want to sync ESXi host just prevent the big time different then trig problem when we reboot any one of the virtual machines.

I think there are lots of reasons to have accurate time. If you use a directory service LDAP or Active Directory there are situations where they may stop functioning or replicating. How would you know when a security issue really occured and how would you track it over time? Or a hardware issue? Perhaps you don't need to conform to financial or government regulation but perhaps some department within your organization or a company you do have relations with might. I would be very careful about dismissing the value of very accurate time.

-- David -- VMware Communities Moderator
0 Kudos
Highlighted
Contributor
Contributor

Hi LucD,

Thanks for that!

0 Kudos
Highlighted
Contributor
Contributor

Hi DSTAVERT,

Thanks for the information, it is another topic.

0 Kudos
Highlighted
Contributor
Contributor

Hi Luc,

I'm getting an error with get-view command as below. We're using ESXi5 and running from powerCLI 4.1. Pls help me.

Get-View : Cannot validate argument on parameter 'Id'. The argument cannot be null or empty.
At line:1 char:19
+ $dtSystem=Get-View <<<<  $esx.ExtensionData.ConfigManager.dateTimeSystem
    + CategoryInfo          : InvalidData: (:) [Get-View], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.Commands.DotNetInterop.GetVIView

0 Kudos
Highlighted
User Moderator
User Moderator

I would suggest to upgrade your PowerCLI if possible.

I can't recreate that error in my environment with PowerCLI 5.1 R2 I'm afraid.


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

0 Kudos