I read a great post by Roger Lund today that discussed timekeeping in VMware virtual machines. That got me thinking, “How would this be handled with PowerCLI?” A blog post was born. 🙂
The first thing that I did was to check the NTP server settings on all of my VMware servers.
[powershell]
$vmhosts = Get-VMHost
$details = @()
foreach ($vmhost in $vmhosts) {
$detail = "" | select Name,NTPServer
$detail.Name = $vmhost.Name
$detail.NTPServer = Get-VMHostNtpServer $vmhost
$details += $detail
}
[/powershell]
Pretty straightforward. Retrieve all of the VMhosts within vCenter, create an array to hold the results, then begin cycling thru all of the hosts and add their hostnames and NTP server settings into the results array. The results come out looking like this:
[code]
Name NTPServer
—- ———
esx01a.domain.com domain.com
esx01b.domain.com domain.com
esx01c.domain.com
esx01d.domain.com domain.com
[/code]
So, what do you do if you want to change the host that is missing the NTP server settings?
[powershell]
Get-VMHost -Name esx01c.domain.com | Add-VmHostNtpServer -NtpServer "domain.com"
Get-VMHostService esx01c.domain.com | Where-Object {$_.key -eq "ntpd"} | Restart-VMHostService -Confirm:$false
[/powershell]
There you have it. Isn’t PowerCLI great? 🙂
Hans De Leenheer (@hansdeleenheer) says
you should add:
foreach
if empty (or )
add-vmhostntp ….
K F says
in PowerCLI 4.1 i get
Get-VMHostNtpServer : 11/18/2010 8:59:45 PM Get-VMHostNtpServer Index was outside the bounds of the array.
At line:4 char:40
+ $detail.NTPServer = Get-VMHostNtpServer <<<< -vmhost $vmhost
+ CategoryInfo : NotSpecified: (:) [Get-VMHostNtpServer], VimException
+ FullyQualifiedErrorId : Core_BaseCmdlet_UnknownError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Host.GetVMHostNtpServer
any ideas what im doing wrong?
Damian Karlson says
What version of ESXi are you running the script against? Mine is 4.1