PowerCLI Script to Edit ESXi Host NTP Settings

Here's another quick one.  A customer of mine recently had to change their NTP settings across every ESXi host in their environment.  Given that they have a lot of clusters with different storage and network settings, host profiles didn't seem to be the best solution available.  Fortunately, PowerCLI has a few nice easy cmdlets to make life easier.  After a bit of quality time on google and the PowerCLI Cmdlet Reference Page, I was able to put together the following quick script.  It goes through every ESXi host in the environment, removes all configured NTP servers, adds whatever servers are specified in the $NTPServers array, sets the service to "on" (this site had some inconsistent configurations there) and restarts the service to ensure that the settings take effect.

If you want to make use of any of this, just change the variables near the top to be a list of your NTP Servers of choice.  If you want to limit the scope of the script, you can change the $AllHosts = Get-VMHost line to be something more specific.  You could "Get-VMHost <MySpecialESXServer>" to only target MySpecialESXServer, or you can just go wild (by which I mean, use wildcards)!

As always, this is posted for educational purposes only and has no guarantee, use it at your own risk.  As always, beware of unintended line breaks due to blog formatting.

#Edit the $NTPServers variable to create a comma delimited list of the NTP Servers that your ESXi hosts should use
$NTPServers = "", ""
#Edit this Get-VMHost command to limit the scope of the script.  For example, "Get-VMHost *test*" will only target hosts with "test" in their name
$AllHosts = Get-VMHost

#No need to edit anything below this line
foreach ($ThisHost in $AllHosts){
    $AllNTP = get-vmhostntpserver -VMHost $ThisHost
    foreach ($ThisNTP in $AllNTP){
        echo "Removing $ThisNTP from $ThisHost"
        remove-vmhostntpserver -VMHost $ThisHost -ntpserver $ThisNTP -Confirm:$false
    foreach ($ThisNTP in $NTPServers){
        echo "Adding $ThisNTP to $ThisHost"
        add-vmhostntpserver -VMHost $ThisHost -ntpserver $ThisNTP -Confirm:$false
    Get-VMHostService -VMHost $ThisHost | where{$_.Key -eq "ntpd"} | restart-vmhostservice -Confirm:$false
    Get-VMHostService -VMHost $ThisHost | where{$_.Key -eq "ntpd"} | set-vmhostservice -policy "on" -Confirm:$false


  1. How do you also force the use of NTP v3 instead of 4 (the default) in this script?

    The VMware KB only mentions editing /etc/ntp.conf which is ugly for large environments.

  2. Sorry, I've never had to force NTP v3 and so don't know an easy way to do it. If you're just appending a line to /etc/ntp.conf, you could use the process that I outlined in yesterday's post: http://virtuallyjason.blogspot.com/2013/09/monitoring-netgp-heap-en-masse.html to just create a bunch of "SSH $ThisHost echo 'your statement' >> /etc/ntp.conf" lines. Or, just copy a fully configured ntp.conf file over the existing ones in your environment.


Post a Comment

Sorry guys, I've been getting a lot of spam recently, so I've had to turn on comment moderation. I'll do my best to moderate them swiftly after they're submitted,

Popular posts from this blog

Deleting Orphaned (AKA Zombie) VMDK Files

Clone a Standard vSwitch from one ESXi Host to Another

Orphaned VMDK Files