Update NTP configuration on multiple ESXi 5.0 hosts

We recently upgraded our NTP infrastructure and I had to reconfigure 46x ESXi 5.0 hosts to reflect this change. I’m not keen on doing these types of operations manually so I wrote this script to automate the process.

I’m sure there are definitely more elegant scripts available, but this one works perfectly well. Just change the highlighted rows to reflect your own environment;


PowerShell Script;
## START ##

## Connect to vCenter
$vcenter = 'myvcenter.mydomain.fqdn'
Write-Host "Connecting to $vcenter" -ForegroundColor Green
Connect-VIServer $vcenter | Out-Null

## OLD NTP servers
$ntp1_old = 'old.ntp.server1'
$ntp2_old = 'old.ntp.server2'
$ntp3_old = 'old.ntp.server3'
$ntp4_old = 'old.ntp.server4'

## NEW NTP servers
$ntp1_new = 'new.ntp.server1'
$ntp2_new = 'new.ntp.server1'
$ntp3_new = 'new.ntp.server1'

## Hosts to Configure
$vmHosts =  Get-Datacenter 'MY-DC' | Get-Cluster 'MY-CLUSTER' | Get-VMHost *

## Action for Each Host
ForEach ($vmHost in $vmHosts) {

Write-Host $vmHost -BackgroundColor Red

## Remove existing NTP servers
Write-Host " - Removing existing NTP servers" -ForegroundColor Yellow
Remove-VmHostNtpServer -NtpServer $ntp1_old -VMHost $vmHost -Confirm:$false | Out-Null
Remove-VmHostNtpServer -NtpServer $ntp2_old -VMHost $vmHost -Confirm:$false | Out-Null
Remove-VmHostNtpServer -NtpServer $ntp3_old -VMHost $vmHost -Confirm:$false | Out-Null
Remove-VmHostNtpServer -NtpServer $ntp4_old -VMHost $vmHost -Confirm:$false | Out-Null

## Add new NTP servers
Write-Host " - Adding New NTP servers" -ForegroundColor Green
Add-VmHostNtpServer -NtpServer $ntp1_new -VMHost $vmHost | Out-Null
Add-VmHostNtpServer -NtpServer $ntp2_new -VMHost $vmHost | Out-Null
Add-VmHostNtpServer -NtpServer $ntp3_new -VMHost $vmHost | Out-Null

## Restart NTP Service
Write-Host " - Restarting the NTP service" -ForegroundColor Yellow
Get-VmHostService -VMHost $vmHost | Where-Object {$_.key -eq "ntpd"} | Restart-VMHostService -Confirm:$false | Out-Null 

## Update Complete
Write-Host " - NTP Servers updated" -ForegroundColor Green
Write-Host ""

## Disconnect from vCenter
Disconnect-VIServer -Server * -Force -Confirm:$false -ErrorAction SilentlyContinue | Out-Null

## END ##

14,792 total views, 0 views today

Share on LinkedInShare on FacebookTweet about this on TwitterShare on Google+Digg thisShare on RedditPin on PinterestEmail this to someonePrint this page

Speak Your Mind