How can I set NTP server settings on a group of ESXi hosts using powerCLI? I want to set my primary and secondary NTP server names, and configure them all to have the NTP server service start with the host.
Thanks!
Like this
Get-VMHost | Add-VMHostNtpServer -NtpServer myntpserver
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Like this
Get-VMHost | Add-VMHostNtpServer -NtpServer myntpserver
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
great thanks. Is it also possible to set the value for making the ntp client service start automatically with the host as opposed to having to be manually started when the host restarts?
This set of commands is aprt of our standard build process.
#Configure NTP server
Add-VmHostNtpServer -VMHost $esx -NtpServer servername
#Allow NTP queries outbound through the firewall
Get-VMHostFirewallException -VMHost $esx | where {$_.Name -eq
"NTP client"
} | Set-VMHostFirewallException -Enabled:$
true
#Start NTP client service and set to automatic
Get-VmHostService -VMHost $esx | Where-Object {$_.key -eq
"ntpd"
} | Start-VMHostService
Get-VmHostService -VMHost $esx | Where-Object {$_.key -eq
"ntpd"
} | Set-VMHostService -policy
"automatic"
Hi ,
use these commands
Check and COnfigure NTP on all ESXI hosts
> Get-VMHost | Get-VmHostService | Where-Object {$_.key -eq "ntpd"}
Key Label Policy Running Required
--- ----- ------ ------- --------
ntpd NTP Daemon on True False
ntpd NTP Daemon on True False
ntpd NTP Daemon off False False
ntpd NTP Daemon off False False
> Get-VMHost | Get-VMHostFirewallException | where {$_.Name -eq "NTP client"}
Name Enabled OutgoingPorts Protocols ServiceRunning
---- ------- ------------- --------- --------------
NTP Client True 123 UDP True
NTP Client True 123 UDP True
NTP Client False 123 UDP False
NTP Client False 123 UDP False
NTP Client False 123 UDP False
>Get-VMHost | Add-VMHostNtpServer 192.168.1.1
> Get-VMHost | Get-VMHostFirewallException | where {$_.Name -eq "NTP client"} |Set-VMHostFirewallException -Enabled:$true
Name Enabled OutgoingPorts Protocols ServiceRunning
---- ------- ------------- --------- --------------
NTP Client True 123 UDP True
NTP Client True 123 UDP True
NTP Client True 123 UDP False
NTP Client True 123 UDP False
> Get-VMHost | Get-VmHostService | Where-Object {$_.key -eq "ntpd"} | Start-VMHostService
Key Label Policy Running Required
--- ----- ------ ------- --------
ntpd NTP Daemon on True False
ntpd NTP Daemon on True False
ntpd NTP Daemon off True False
ntpd NTP Daemon off True False
> Get-VMhost | Get-VmHostService | Where-Object {$_.key -eq "ntpd"} | Set-VMHostService -policy "automatic"
Key Label Policy Running Required
--- ----- ------ ------- --------
ntpd NTP Daemon automatic True False
ntpd NTP Daemon automatic True False
ntpd NTP Daemon automatic True False
Note:- Get-VMhost "ESXIHOSTNAME" , if you use this you can apply these setting only to that HOST .
visit for more :- vSphere Advanced Technics: Work With vSphere Power CLI
Great thanks!
Thanks all!
Here's a slight modification which will show the respective VMhost:
> Get-VMHost | Get-VmHostService | Where-Object {$_.key -eq "ntpd"} | select vmhost, key, label, policy, running, required | Format-Table
VMHost Key Label Policy Running Required
------ --- ----- ------ ------- --------
10.10.10.139 ntpd NTP Daemon on True True
10.10.10.147 ntpd NTP Daemon on True True
10.10.10.140 ntpd NTP Daemon on True True
10.10.10.141 ntpd NTP Daemon off False False
10.10.10.146 ntpd NTP Daemon off False False
10.10.10.142 ntpd NTP Daemon off False False
10.10.10.145 ntpd NTP Daemon off False False
10.10.10.143 ntpd NTP Daemon off False False
10.10.10.144 ntpd NTP Daemon off False False
To those of you who are looking for a way of scripting this please review the following information.
Firstly you create a new file called Set-NTP_ESXi-Hosts.csv.
In this file you place all of the ESXi hosts you want this NTP settings to be applied to.
hostname
host-name-001.DOMAIN.TLD
host-name-002.DOMAIN.TLD
host-name-003.DOMAIN.TLD
host-name-004.DOMAIN.TLD
host-name-005.DOMAIN.TLD
...
Secondly you save the following script as Set-NTP.ps1.
Clear-Host
$FilePath = "C:\your\path\to\the\PowerCLI-Script\Set-NTP_ESXi-Hosts.csv"
$FileContent = Import-CSV $FilePath
Foreach ($CsvLine in $FileContent)
{
$esx = $CsvLine.hostname
# Add NTP servers separately
Add-VmHostNtpServer -VMHost $esx -NtpServer ntpserver1.ntpserver.com
Add-VmHostNtpServer -VMHost $esx -NtpServer ntpserver2.ntpserver.com
Add-VmHostNtpServer -VMHost $esx -NtpServer ntpserver3.ntpserver.com
# Set the firewall regulation to allow traffic for NTP lookup
Get-VMHostFirewallException -VMHost $esx | where {$_.Name -eq "NTP client"} | Set-VMHostFirewallException -Enabled:$true
# Start NTP daemon and make it start automatically when needed
Get-VmHostService -VMHost $esx | Where-Object {$_.key -eq "ntpd"} | Start-VMHostService
Get-VmHostService -VMHost $esx | Where-Object {$_.key -eq "ntpd"} | Set-VMHostService -policy "automatic"
}
As you can see from line 11 on you specify the NTP servers to be passed on to each ESXi host given in the .csv file.
The rest is a combination of what's above: read the .csv file, loop through its entries, sets the NTP settings per host for as long as it find lines (and the servers of course) and that's it.
Of course you need to be connected to your vCenter first. This is done by the Connect-VIServer command, as you all know.
Maybe this is of help when you have a list of many servers (as I had, app. 80) where these settings should be applied.
Thanks Raj,
can you please explain what is that value "False" in heading "Required" means ?
Key Label Policy Running Required
--- ----- ------ ------- --------
ntpd NTP Daemon on True False