# thanks https://www.kmruddy.com/2013/powercli-reset-ntp-settings-on-vmhosts-per-cluster/
# tweaked by https://vmscribble.com
# What is the vCenter and ESXI FQDN?
$vCenter=Read-Host"Enter the vCenter FQDN"
$InputCluster=Read-Host"Enter the Cluster Name"
# What are the 2 new NTP servers?
$InputNTP1=Read-Host"Enter the 1st New NTP Server"
$InputNTP2=Read-Host"Enter the 2nd New NTP Server"
#NTP servers to be changed to
$ntp1=$InputNTP1
$ntp2=$InputNTP2
#Check with end user before connecting to the VC
Write-Host""
Read-Host"
--> Hit ENTER to proceed to:
1. Remove the current NTP servers from all ESXi hosts in Cluster $InputCluster
2. Add NTP servers $InputNTP1 and $InputNTP2
"
Write-Host""
# Connect to your vCenter
Connect-VIServer$vCenter|out-null
#Select Cluster to change NTP Settings
$Cluster=Get-Cluster$InputCluster
#Grabbing VMHosts for desired Cluster
$allVMhost=$Cluster|Get-VMHost|sortName
#Reseting NTP servers one by one
foreach($vmhostin$allVMhost){
#Remove existing NTP servers
Write-Host"--> $vmhost"-foregroundcolorGreen
Write-Host"Step 1 - Removing all NTP Servers from $vmhost"-foregroundcolorGray
$allNTPList=Get-VMHostNtpServer-VMHost$vmhost
Remove-VMHostNtpServer-VMHost$vmhost-NtpServer$allNTPList-Confirm:$false|out-null
Write-Host"Step 1 - Confirmed. All NTP Servers from $vmhost have been removed"-foregroundcolorGray
#Setting NTP servers
Write-Host"Step 2 - Adding NTP servers to $vmhost"-foregroundcolorGray
Add-VmHostNtpServer-NtpServer$ntp1,$ntp2-VMHost$vmhost-Confirm:$false|out-null
Write-Host"Step 2 - The following NTP servers have been added to $vmhost : $ntp1, $ntp2"-foregroundcolorGray
#Checking NTP Service on the ESXi host
$ntp=Get-VMHostService-vmhost$vmhost|?{$_.Key-eq'ntpd'}
Set-VMHostService$ntp-Policyon|out-null
if($ntp.Running){
Restart-VMHostService$ntp-confirm:$false
Write-Host"Step 3 - $ntp Service on $vmhost was On and was restarted"-foregroundcolorGray
}
Else{
Start-VMHostService$ntp-confirm:$false
Write-Host"Step 3 - $ntp Service on $vmhost was Off and has been started"-foregroundcolorGray
}
Write-Host""
Write-Host""
}
# list current
Write-Host""
Write-Host"AFTER the change - Current NTP in Cluster $InputCluster"-foregroundcolorGreen
$Cluster|Get-VMHost|sortName|SelectName,@{N="NTPServer";E={$_|Get-VMHostNtpServer}}
# Disconnect from the vCenter
Disconnect-VIServer$vCenter-Confirm:$false