ESXi NTP PowerShell Script

I wanted a quick way to configure NTP settings on a number of VMware ESXi hosts across two datacenters. Hosts in site A should point to the NTP source locally primarily and to site B NTP as a secondary source and vice versa for hosts in site B. The following isn’t perfect and a lot could be done to improve it but considering it’s just a quick way of achieving a task I’m not too bothered.

  • I check to see if the VMware PowerCLI addon is installed in the default location -we could improve this test by checking the registry or another value
  • We define a few variables either statically or by interacting at the prompt with our user
  • Load the PowerCLI plugin, connect to vCenter and start configuring hosts
    • As the hosts in question all follow a specific naming convention the easiest thing to do was just say ‘if my hostname is like x then do this otherwise it must be of type y so do this instead’
  • Once we’ve configured the settings we make sure the service is allowed on the ESXi firewall and that it is set to run automatically

Plenty of ways this could be enhanced but then again we also have tools like host profiles and other techniques to ensure consistent settings across our infrastructure. When you’re just spinning up labs or environments that don’t (license wise) support some of the more advanced stuff then it’s useful to have a few scripts to speed things along.

Note – The IP addresses and hostname structure have been amended to provide examples, they aren’t from my production environment.

 

# Script to configure NTP on ESXi Servers
# Author: Alex Bytes
# Version: 1.0.1

# CHANGELOG
#
# Version 1.0.1 -
# Added variable for vCenter server
# Added if statement to test PowerCLI path and report an error if path not found
#
#
# Version 1.0.0
# Initial release
#

write-host "Please note - this script is designed ONLY to configure NTP settings on Site A and Site B ESXi hosts."

if (test-path -path "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI")
{

# Variable Declarations
$esxi = Read-Host "Please enter the ESXi FQDN"
$siteaNTP = "192.168.50.10"
$sitebNTP = "172.16.50.10"
$vCenterServer = Read-host "Please enter the vCenter Server FQDN"

# Add PowerShell plugin and connect to vCenter Server
Write-Host "Loading PowerCLI and connecting to vCenter"
Add-PSSnapin vmware.vimautomation.core
Connect-VIServer $vCenterServer

# Configure NTP server
write-host "Configuring NTP server addresses"
if ($esxi -ilike "lab-sa*")
{
Add-VmHostNtpServer -VMHost $esxi -NtpServer $siteaNTP
Add-VMHostNtpServer -VMHost $esxi -NtpServer $sitebNTP
}
else
{
Add-VMHostNtpServer -VMHost $esxi -NtpServer $sitebNTP
Add-VMHostNtpServer -VMHost $esxi -NtpServer $siteaNTP
}

# Allow NTP queries outbound through the firewall
Write-Host "Configuring Firewall rules"
Get-VMHostFirewallException -VMHost $esxi | where {$_.Name -eq "NTP client"} | Set-VMHostFirewallException -Enabled:$true

# Start NTP client service and set to automatic
write-host "Configuring services"
Get-VmHostService -VMHost $esxi | Where-Object {$_.key -eq "ntpd"} | Start-VMHostService
Get-VmHostService -VMHost $esxi | Where-Object {$_.key -eq "ntpd"} | Set-VMHostService -policy "automatic"
}
else
{
write-host "VMware PowerCLI is required for this script to function - please install VMware PowerCLI and try again (http://blogs.vmware.com/PowerCLI/)"
}

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.