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:

foreach($esxcli in get-vmhost|get-esxcli){""|select @{n='Time';e={$esxcli.system.time.get()}},@{n='hostname';e={$esxcli.system.hostname.get().hostname}} }

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:

$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
}
Advertisements

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

  1. 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”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s