How to use PowerCli to update the NTP Servers on all VMHosts at once.

We have been doing a massive consolidation at the office the last few months. With this consolidation we have been getting VMHosts that haven’t been configured with the new standard configuration, so we have to fix the issues. The biggest issue is the NTP servers not being set the same servers. So how to you fix it, script it.

$oldntpservers='192.168.0.1','192.168.0.2'
$newntpservers='0.vmware.pool.ntp.org','1.vmware.pool.ntp.org','2.vmware.pool.ntp.org'
foreach($vmhost in get-vmhost){
#stop ntpd service
$vmhost|Get-VMHostService |?{$_.key -eq 'ntpd'}|Stop-VMHostService -Confirm:$false
#remove ntpservers
$vmhost|Remove-VMHostNtpServer -NtpServer $oldntpservers -Confirm:$false
#add new ntpservers
$vmhost|Add-VmHostNtpServer -NtpServer $newntpservers
#start ntpd service
$vmhost|Get-VMHostService |?{$_.key -eq 'ntpd'}|Start-VMHostService
}
 $oldntpservers='192.168.0.1','192.168.0.2' 

This is the list of all of the NTP servers that are currently configured in the environment. Get all of them, it doesn’t matter if they are not all configured on all of the Hosts.

 $newntpservers='0.vmware.pool.ntp.org','1.vmware.pool.ntp.org','2.vmware.pool.ntp.org' 

This is the list of the NTP servers that you want to use in the environment.

 foreach($vmhost in get-vmhost){ 

This will get a list of all of the VMHosts and run through them one at a time

 $vmhost|Get-VMHostService |?{$_.key -eq 'ntpd'}|Stop-VMHostService -Confirm:$false 

This takes the VMHost and get the NTP service that and stops it

 $vmhost|Remove-VMHostNtpServer -NtpServer $oldntpservers -Confirm:$false 

This line removes the NTP servers from the VMHhost based on the list of $oldntpservers

 $vmhost|Add-VmHostNtpServer -NtpServer $newntpservers 

This line adds the new NTP servers to the VMHost base on the list of $newntpservers

 $vmhost|Get-VMHostService |?{$_.key -eq 'ntpd'}|Start-VMHostService 

This line starts the NTP service on the VMHost

Leave a Reply

Your email address will not be published. Required fields are marked *