How to check time on ESXi via esxcli, changing ntp servers, restarting ntpd service

by Grzegorz Kulikowski

If we would like to check what time do our hosts have we can write a simple one liner:
[sourcecode language=”powershell”]
foreach($esxcli in get-vmhost|get-esxcli){""|select @{n=’Time’;e={$esxcli.system.time.get()}},@{n=’hostname’;e={$esxcli.system.hostname.get().hostname}} }
[/sourcecode]
Output should be similar to:

So what we are actually doing is:
1) get access to all hosts esxcli
2) on each of those esxcli to run command: esxcli system time get and esxcli system hostname get
3) output with columns time and hostname
This will output time from all our esxi host systems that are registered in the virtual center to which we are connected in current powercli session.
If you do not want to it for all hosts simply manipulate the get-vmhost , like get-vmhost -Location ‘xxxx’

I also had to change ntp servers for my hosts so:
[sourcecode language=”powershell”]
$oldntpservers=’192.168.0.1′,’192.168.0.2′
$newntpservers=’192.168.0.20′,’192.168.0.21′
foreach($vmhost in get-vmhost){
#stop ntpd service
$vmhost|Get-VMHostService |?{$_.key -eq ‘ntpd’}|Stop-VMHostService -Confirm:$false
#remove ntpservers
$vmhost|Remove-VMHostNtpServer -NtpServer $oldntpservers -Confirm:$false
#add new ntpservers
$vmhost|Add-VmHostNtpServer -NtpServer $newntpservers
#start ntpd service
$vmhost|Get-VMHostService |?{$_.key -eq ‘ntpd’}|Start-VMHostService
}
[/sourcecode]

You may also like

1 comment

Geoff September 19, 2013 - 2:54 pm

Thanks I just used this as a reference to reconfigure ntp on our servers. The only thing I’ll add is I ran one more command to make the ntp server startup type “automatic” instead of manual:
$vmhost | get-vmhostservice | ?{$_.key -eq ‘ntpd’} | set-vmhostservice -Policy “Automatic”

Reply

Leave a Reply

Chinese (Simplified)EnglishFrenchGermanHindiPolishSpanish
Streaming live on Twitch right now.
CURRENTLY OFFLINE