Close Menu
PowerShell.roPowerShell.ro
  • Home
  • Automation
  • Powershell
  • VMWare
  • Windows
LinkedIn X (Twitter) Facebook
PowerShell.roPowerShell.ro
  • Home
  • Automation
  • Powershell
  • VMWare
  • Windows
LinkedIn Facebook X (Twitter)
PowerShell.roPowerShell.ro
Home»Automation»Check the NTP Server on all ESXi Hosts via Powershell
Automation

Check the NTP Server on all ESXi Hosts via Powershell

Catalin CristescuBy Catalin CristescuJuly 26, 2023
Share Facebook Twitter Pinterest LinkedIn Tumblr Reddit Telegram Email
Share
Facebook Twitter LinkedIn Pinterest Email

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.

Script

#requires -modules VMware.VimAutomation.Core
 
param (
    [CmdletBinding()]
    [Parameter(Mandatory = $true)]
    [string]$VCenter,
      
    [Parameter(Mandatory = $true)]
    [string]$Username,
      
    [Parameter(Mandatory = $true)]
    [string]$Password,
      
    [Parameter(Mandatory = $false)]
    [string]$Email
)
  
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
    Continue
}
catch [VMware.VimAutomation.Sdk.Types.V1.ErrorHandling.VimException.ViServerConnectionException] {
    Write-Host "Cannot connect to $VCenter - check IP/FQDN" -ForegroundColor Red
    Continue
}
catch {
    Write-Host "Cannot connect to $VCenter - Unknown error" -ForegroundColor Red
    Continue
}

$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 = "noreply@youremail.com"
    $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.To.Add($Email)
    $Message.From = $From
    $Message.Attachments.Add($AttachmentFile)
    $SMTP = New-Object System.Net.Mail.SmtpClient $SMTPServer
    $SMTP.Send($Message)
}

Disconnect-VIServer -Server $vcenter -Confirm:$false
ntp server ntp service powercli powershell script
Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
Previous ArticlePowerCLI: Get Storage Paths of ESXi servers from vCenter
Next Article Import custom SSL Certificate on HPE ILO through Powershell script
Catalin Cristescu
  • Website

Related Posts

Automation

PowerShell script for automated remediation for CrowdStrike issue

July 20, 2024
VMWare

vExperts stats 2024

May 27, 2024
Automation

Tagging Virtual Machines with the CISTag Module in PowerShell

February 1, 2024

Comments are closed.

Recent Posts
  • PowerShell script for automated remediation for CrowdStrike issue
  • vExperts stats 2024
  • Tagging Virtual Machines with the CISTag Module in PowerShell
  • Update HPE ILO firmware using PowerShell
  • PowerCLI script to check Cluster Overcommitment ratio
Categories
  • Automation (11)
  • Powershell (14)
  • Uncategorized (1)
  • VMWare (15)
  • Windows (2)
About Powershell.ro

Powershell.ro, the ultimate hub for tech enthusiasts! Dive into expert-written articles on PowerShell scripts, VMware, automation, Microsoft technologies, and DevOps practices. Unlock tips, tutorials, and solutions designed to empower IT professionals and boost your skills in automation and infrastructure management.

Recent Posts
  • PowerShell script for automated remediation for CrowdStrike issue
  • vExperts stats 2024
  • Tagging Virtual Machines with the CISTag Module in PowerShell
  • Update HPE ILO firmware using PowerShell
  • PowerCLI script to check Cluster Overcommitment ratio
LinkedIn
© 2025 PowerShell.ro.

Type above and press Enter to search. Press Esc to cancel.