QuickTip:Manage VM NTP with PowerCLI

One thing that can cause a lot of headaches for a VM and an infrastructure in general is incorrect NTP configuration.

Fortunately for a VM, there is mainly one setting for NTP outside of guest OS…Whether you want to get your time(NTP) from the vm’s host.

There are really only two reasons you want to get your VM’s time from its host. Those being, you don’t want to to query your NTP server for the time, or it can’t query the NTP server for the time.

Yes, I know, those are pretty vague, but also fairly all-encompassing :). Let’s do an example. Let’s say your environment consists of 1 bajillion!!!! VMs, if you only have one NTP server, it is very possible you don’t want all of those VM constantly querying the NTP server to get their time. In this situation, it is less network intensive if they simply query their hosts. Secondly, it is possible that a particular vm, is logically segmented from the NTP server and the VM is not allowed to communicate with the NTP server, again in this case it makes a lot of sense to get the time from its host.

Great! So what is the Setting I am talking about?  Well, right-click on a VM, Edit Settings-Options Tab-VMware Tools, and under the Advanced section you will see “Synchronize guest time with host”. If the box is checked it will query its host for its time.

So how do I quickly find out what all the VM settings are? Glad you asked 🙂


get-vm|select name, @{N="SyncWithHost";E={$_.extensiondata.config.tools.synctimewithhost}}

That quick one-liner will display all the VMs of the server you are connected to, and whether they get their time from their host(True) or not (False)

That’s so sweet vNoob

I know I know, no autographs right now, I need to finish this post.

K, SOoooOOOoOoo, Let’s say once you find out what VMs the current NTP configuration for the VMs, that you want to change some or all of them.


param($vm="*", $sync=$false)

$spec = new-object VMware.Vim.VirtualMachineConfigSpec
$spec.tools = New-Object VMware.Vim.ToolsConfigInfo
$spec.tools.syncTimeWithHost = $sync

$vms=get-vm $vm |?{$_.extensiondata.config.tools.synctimewithhost -like $true}
$vms|select -expand name
$vms|%{$_.extensiondata.reconfigvm_task($spec)}

By default this script will change all your VMs to Not sync with their host. However I threw two variables in there, so you can select your VMs individually, or set them to true.

An example might be

./configtimesync.ps1 -vm MyVM -sync $True

This will set the vm MyVM to sync with its host($True)

./configtimesync.ps1

This will set all VMs to Not sync with their host.

I Hope This Helps!

Also download the file if you are interested!

ConfigTimeSync
ConfigTimeSync
configtimesync.ps1
Version: 1
336.0 B
664 Downloads
Details...

One Response to QuickTip:Manage VM NTP with PowerCLI

Leave a Reply