I have created a function to configure NTP as part of build process on all esxi hosts within a cluster (checks if it is set currently). I have a the following snippet of powercli (within the loop of the hosts). It will set the NTP server, set the policy, and restarts the service. The one thing it doesnt do is enable the NTP client. If you go to Time Configuration, it is still set to Manual. But you see the NTP service running, the NTP server set, and the policy set. How to I enable to the NTP Client?
try {
$currentNtpServers = Get-VMHostNtpServer -VMHost $esxiHost
if ($currentNtpServers -contains $NtpServer) {
Write-LogAndHost " NTP server '$NtpServer' is already configured on $($esxiHost.Name)." -logFile $logFile
} else {
Add-VMHostNtpServer -VMHost $esxiHost -NtpServer $NtpServer -Confirm:$false | Out-Null
Write-LogAndHost " NTP server '$NtpServer' has been configured on $($esxiHost.Name)." -logFile $logFile
}
} catch {
Write-LogAndHost "Error occurred while setting NTP server on $($esxiHost.Name): $_" -logFile $logFile
}
$ntpService = Get-VMHostService -VMHost $esxiHost | Where-Object {$_.Key -eq "ntpd"}
if ($ntpService.Policy -ne "on") {
Set-VMHostService -HostService $ntpService -Policy "on" -Confirm:$false | Out-Null
Write-LogAndHost " NTP service startup policy set to 'on' on $($esxiHost.Name)." -logFile $logFile
}
if (-not $ntpService.Running) {
Start-VMHostService -HostService $ntpService -Confirm:$false | Out-Null
Write-LogAndHost " NTP service enabled and started on $($esxiHost.Name)." -logFile $logFile
} else {
Write-LogAndHost " NTP service is already running on $($esxiHost.Name)." -logFile $logFile
}
Restart-VMHostService -HostService $ntpService -Confirm:$false | Out-Null
Write-LogAndHost " NTP service restarted on $($esxiHost.Name)." -logFile $logFile
}
Did you try setting the Policy to automatic?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks for the suggestion. One thing I neglected to do is I also restarted the hosts (after these commands) to no avail. Based on you suggestion, I added the following line.
# Set the NTP service policy to "Automatic"
Set-VMHostService -HostService $ntpService -Policy "Automatic" -Confirm:$false | Out-Null
Now, the client was still showing Disabled. I restart the host then, and the client itself was then enabled when the host came up. However, the weird thing is I can set it manually (again, choosing the radio button 'Use Network Time Protocol (Enable Client)' when editing the configuration, and it will show enabled with no restart. I think I am still missing something in the powercli.
Not sure what exactly you changed, but this seems to be working for me (without a rebooting the ESXi node).
try {
$currentNtpServers = Get-VMHostNtpServer -VMHost $esxiHost
if ($currentNtpServers -contains $NtpServer) {
Write-LogAndHost " NTP server '$NtpServer' is already configured on $($esxiHost.Name)." -logFile $logFile
} else {
Add-VMHostNtpServer -VMHost $esxiHost -NtpServer $NtpServer -Confirm:$false | Out-Null
Write-LogAndHost " NTP server '$NtpServer' has been configured on $($esxiHost.Name)." -logFile $logFile
}
} catch {
Write-LogAndHost "Error occurred while setting NTP server on $($esxiHost.Name): $_" -logFile $logFile
}
$ntpService = Get-VMHostService -VMHost $esxiHost | Where-Object { $_.Key -eq "ntpd" }
if ($ntpService.Policy -ne "Automatic") {
Set-VMHostService -HostService $ntpService -Policy "Automatic" -Confirm:$false | Out-Null
Write-LogAndHost " NTP service startup policy set to 'on' on $($esxiHost.Name)." -logFile $logFile
}
if (-not $ntpService.Running) {
Start-VMHostService -HostService $ntpService -Confirm:$false | Out-Null
Write-LogAndHost " NTP service enabled and started on $($esxiHost.Name)." -logFile $logFile
} else {
Write-LogAndHost " NTP service is already running on $($esxiHost.Name)." -logFile $logFile
}
Restart-VMHostService -HostService $ntpService -Confirm:$false | Out-Null
Write-LogAndHost " NTP service restarted on $($esxiHost.Name)." -logFile $logFile
If that doesn't work for you, what output are you getting?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference