Site icon Grzegorz Kulikowski

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

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]