PowerCLI Remove NTP and Add New NTP to ESXi Cluster

PowerCLI

You have an ESXi cluster that has a mixed bag of NTP servers (messy).

Most PowerCLI scripts will add NTP servers but, not remove.  If they remove, you have to specify the old ones to remove.  In this case the cluster is so mixed with NTP servers, it would take more time to create the list to add to the script.

This interactive script will remove all NTP servers, add 2 new ones and restart the NTPD service.

This is a modification of https://www.kmruddy.com/2013/powercli-reset-ntp-settings-on-vmhosts-per-cluster/

# 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 | sort Name 

#Reseting NTP servers one by one 
foreach ($vmhost in $allVMhost){ 
    #Remove existing NTP servers 
    Write-Host "--> $vmhost" -foregroundcolor Green
    Write-Host "Step 1 - Removing all NTP Servers from $vmhost" -foregroundcolor Gray
    $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" -foregroundcolor Gray

    #Setting NTP servers 
    Write-Host "Step 2 - Adding NTP servers to $vmhost" -foregroundcolor Gray
    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" -foregroundcolor Gray

    #Checking NTP Service on the ESXi host 
    $ntp = Get-VMHostService -vmhost $vmhost| ? {$_.Key -eq 'ntpd'}
    Set-VMHostService $ntp -Policy on | out-null 
    if ($ntp.Running ){ 
        Restart-VMHostService $ntp -confirm:$false 
        Write-Host "Step 3 - $ntp Service on $vmhost was On and was restarted" -foregroundcolor Gray
    } 
    Else{ 
        Start-VMHostService $ntp -confirm:$false 
        Write-Host "Step 3 - $ntp Service on $vmhost was Off and has been started" -foregroundcolor Gray
    }
    Write-Host ""
    Write-Host ""
}


# list current
Write-Host ""
Write-Host "AFTER the change - Current NTP in Cluster $InputCluster" -foregroundcolor Green
$Cluster | Get-VMHost | sort Name | Select Name, @{N="NTPServer";E={$_ |Get-VMHostNtpServer}}


# Disconnect from the vCenter
Disconnect-VIServer $vCenter -Confirm:$false
Situation

You have an ESXi cluster that has a mixed bag of NTP servers (messy).

Most PowerCLI scripts will add NTP servers but, not remove.  If they remove, you have to specify the old ones to remove.  In this case the cluster is so mixed with NTP servers, it would take more time to create the list to add to the script.

This interactive script will remove all NTP servers, add 2 new ones and restart the NTPD service.

This is a modification of https://www.kmruddy.com/2013/powercli-reset-ntp-settings-on-vmhosts-per-cluster/

Script
# 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 | sort Name 

#Reseting NTP servers one by one 
foreach ($vmhost in $allVMhost){ 
    #Remove existing NTP servers 
    Write-Host "--> $vmhost" -foregroundcolor Green
    Write-Host "Step 1 - Removing all NTP Servers from $vmhost" -foregroundcolor Gray
    $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" -foregroundcolor Gray

    #Setting NTP servers 
    Write-Host "Step 2 - Adding NTP servers to $vmhost" -foregroundcolor Gray
    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" -foregroundcolor Gray

    #Checking NTP Service on the ESXi host 
    $ntp = Get-VMHostService -vmhost $vmhost| ? {$_.Key -eq 'ntpd'}
    Set-VMHostService $ntp -Policy on | out-null 
    if ($ntp.Running ){ 
        Restart-VMHostService $ntp -confirm:$false 
        Write-Host "Step 3 - $ntp Service on $vmhost was On and was restarted" -foregroundcolor Gray
    } 
    Else{ 
        Start-VMHostService $ntp -confirm:$false 
        Write-Host "Step 3 - $ntp Service on $vmhost was Off and has been started" -foregroundcolor Gray
    }
    Write-Host ""
    Write-Host ""
}


# list current
Write-Host ""
Write-Host "AFTER the change - Current NTP in Cluster $InputCluster" -foregroundcolor Green
$Cluster | Get-VMHost | sort Name | Select Name, @{N="NTPServer";E={$_ |Get-VMHostNtpServer}}


# Disconnect from the vCenter
Disconnect-VIServer $vCenter -Confirm:$false