Check the NTP Server on all ESXi Hosts via Powershell

An effective approach for checking the NTP server and NTP Service on all ESXi hosts from a vCenter server is through a Powershell script.

This Powershell script has been developed to perform the following tasks:

  1. Establish a connection to a vCenter server.
  2. Query all the ESXi hosts to retrieve the configured NTP Server information.
  3. Query all the ESXi hosts to obtain the status of the NTP Service.
  4. Compile the gathered information into a list and dispatch it via mail to a designated distribution list.

To automate the process, the script can be configured to run as a Scheduled Task on a Windows server.

Furthermore, for scalability, the script can be modified with the option to set up a list of vCenters and iterate through them using a foreach loop in the script.


#requires -modules VMware.VimAutomation.Core
param (
    [Parameter(Mandatory = $true)]
    [Parameter(Mandatory = $true)]
    [Parameter(Mandatory = $true)]
    [Parameter(Mandatory = $false)]
Import-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue
Set-PowerCLIConfiguration -ProxyPolicy NoProxy -DefaultVIServerMode multiple -Scope User -InvalidCertificateAction ignore -Confirm:$false | Out-Null
# Connection to vCenter
try {
    Connect-VIServer -Server $VCenter -User $Username -Password $Password -ErrorAction Stop | Out-Null
catch [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InvalidLogin] {
    Write-Host "Cannot connect to $VCenter with provided credentials" -ForegroundColor Red
catch [VMware.VimAutomation.Sdk.Types.V1.ErrorHandling.VimException.ViServerConnectionException] {
    Write-Host "Cannot connect to $VCenter - check IP/FQDN" -ForegroundColor Red
catch {
    Write-Host "Cannot connect to $VCenter - Unknown error" -ForegroundColor Red

$NTPdetails = Get-VMHost | Sort-Object Name | Select-Object Name, 
           @{Name="NTPServer";Expression={ $_ | Get-VMHostNtpServer }},
           @{Name="ServiceStatus";Expression={(Get-VMHostService -VMHost $_ | Where-Object {$_.key -eq "ntpd" }).Running }}

# Email global variables
    $ReportTime = Get-Date -Format yyyy.M.d
    $MailTitle = "NTP status from $vcenter"

    $SMTPServer = "SMTP_SERVER"
    $From = ""
    $Timeout = "60"
    $Subject = "$MailTitle $((Get-Date).ToShortDateString())"
    $Message = New-Object System.Net.Mail.MailMessage
    $Body = $NTPdetails | ConvertTo-Html
    $Message.Subject = $Subject
    $Message.Body = $Body
    $Message.IsBodyHtml = $true
    $Message.From = $From
    $SMTP = New-Object System.Net.Mail.SmtpClient $SMTPServer

Disconnect-VIServer -Server $vcenter -Confirm:$false