PowerCLI: Reconfiguring NTP Servers on ESX Hosts

Just lately I’ve been creating a lot of PowerCLI scripts to help configure various aspects of out ESX environment. We’ve just implemented a new NTP Server to our network. So I was given the job to update all of our ESX Hosts. I didn’t fancy spending all morning manually changing them, so I set to work in creating a PowerCLI script to do the business. Here is the result.

$Cluster = "<cluster name>" 
$Hosts = Get-Cluster $Cluster | Get-VMHost 
ForEach ($Host in $Hosts) 
{ 
Remove-VmHostNtpServer -NtpServer "<old ntp server>" -VMHost $Host | Out-Null  
Add-VmHostNtpServer -NtpServer "<new ntp server>" -VMHost $Host | Out-Null 
Get-VmHostService -VMHost $Host | Where-Object {$_.key -eq "ntpd"} | Restart-VMHostService -Confirm:$false | Out-Null 
write "NTP Server was changed on $Host" 
}