Bulk configure NTP for vSphere hosts using PowerCLI

Are you setting up a new greenfield environment with VMware vSphere? Or did you introduce a new NTP server in your environment? Read on how to bulk configure this on your ESXi hosts!

Some days ago I was working on a new VMware vCloud environment and had to set up NTP on all ESXi hosts, as the NTP server was introduced after the placement of these hosts.

As you might know, you can synchronise guest OS’s with VMware Tools, which uses the clock of the physical host your VM is running. Therefor, keeping your hosts’ date and time accurate is very important!

Even when you are not synchronising the clock of your guest OS, performing a clean boot (manual or HA) or a vMotion on a particular VM, the guest OS will apply the date and time of your ESXi host.

To prevent having an inaccurate time configuration, use the PowerCLI commands below to configure the ESXi firewall, configure the NTP client and make sure it boots up when your ESXi host does.

Before executing, replace the vCenterServerHostName with your vCenter Server IP or FQDN. I used 192.168.1.1 and 192.168.2.1 as examples to apply as NTP servers, you should input your own (single or multiple).

The commands execute against all your vSphere hosts under the vCenter Server you are connecting to. If you want to configure a single host, use Get-VMHost hostname where hostname is the name of your ESXi box inside vCenter Server.

[code language=”powershell”]

Connect-VIServer vCenterServerHostName
Get-VMHost | Add-VMHostNtpServer 192.168.1.1
Get-VMHost | Add-VMHostNtpServer 192.168.2.1
Get-VMHost | Get-VMHostFirewallException | where {$_.Name -eq "NTP client"} | Set-VMHostFirewallException -Enabled:$true
Get-VMHost | Get-VmHostService | Where-Object {$_.key -eq "ntpd"} | Start-VMHostService
Get-VMhost | Get-VmHostService | Where-Object {$_.key -eq "ntpd"} | Set-VMHostService -policy "automatic"

[/code]

5 thoughts on “Bulk configure NTP for vSphere hosts using PowerCLI

  1. Pingback: Bulk configure NPT Servers

  2. Pingback: Set NTP on vSphere hosts via PowerCLI | Jay's Blog

  3. Pingback: Bulk Edit NTP servers in vSphere using PowerCLI – Site Title

  4. Pingback: POSH1Liner: Set NTP server on all hosts attached to a vCenter | Virtualization, Cloud, Infrastructure and all that stuff in-between

Leave a Reply