How to configure NTP settings on ESXi hosts w/PowerCLi

Cover

PowerCLi includes three functions for configuring «NTP Servers» setting on ESXi hosts: Get-/Add-/Remove-VMHostNtpServer. You can view them using Get-Command cmdlet.

Get-Command –Noun *ntpserver

In my opinion, it is clearly not enough.

Please meet the ​Set-VMHostNtpServer function from my PowerCLi Vi-Module module.

Set-VMHostNtpServer

  • This function combines all three previous functions plus a few more to restart the NTP daemon to apply configuration changes.

Just import the Vi-Module and the new function to your service.

Import-Module Vi-Module -Force
Get-Command –Noun *ntpserver
Get-Alias –Definition Set-VMHostNtpServer

01.Set-VMHostNtpServer

  • Before using this new function, it would be nice to check what the state of things is now. This is easy to do with following one-liner in PowerCLi.
Get-VMHost | sort Parent, Name | select Parent, Name, @{N='NTP'; E={$_ | Get-VMHostNtpServer}} |ogv -Title NTP
Get-VMHost | sort Parent, Name | select Parent, Name, @{N='NTP'; E={($_ | Get-VMHostNtpServer) –join ‘, ‘}} | epcsv –notype .\NTP.csv
  • The function accepts just two parameters: -NewNtp (you can use IP addresses, Hostnames or FQDN) and -VMHost (ESXi hosts can be passed through the pipeline).
Get-VMHost | Set-VMHostNtpServer –NewNtp ntp1, ntp2.contoso.com, 10.1.2.3

The function returns structured objects with the following properties.

Property Description
VMHost ESXi host’s FQDN
OldNtp Previous NTP settings
IsOldRemoved Whether it was possible to remove the previous settings – Boolean value
NewNtp New NTP settings
IsDaemonRestarted Whether it was possible to restart the NTP daemon to apply the new settings immediately – Boolean value

The properties selected in such a way that you can keep track of all the steps of the function.

  • The function won’t be executed on Powered off, Disconnected and inaccessible (Not Responding) hosts.
    Such hosts you can see while using -Verbose parameter.
Get-VMHost | setntp ntp1 -Verbose
  • The Set-VMHostNtpServer supports -Confirm parameter also.
Get-VMHost | Set-VMHostNtpServer ntp1, ntp2 –Confirm:$false

02.Set-VMHostNtpServer

  • The function replaces previous NTP settings with the new settings. If you just need to add new NTP server(s) to the existing ones, use the built-in Add-VMHostNtpServer function.

  • Like all my functions, Set-VMHostNtpServer provided with a sufficient number of examples and content-based help, which you can see by running the following commands.

Get-Help Set-VMHostNtpServer -Full
Get-Help Set-VMHostNtpServer -Examples
Get-Help Set-VMHostNtpServer –Parameter NewNtp

You may also like:

Get-VMHostGPU – Get ESXi GPU info
Get-VMHostFirmwareVersion – Get ESXi servers BIOS/Firmware version

2 thoughts on “How to configure NTP settings on ESXi hosts w/PowerCLi

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