Reply to Message

View discussion in a popup

Replying to:
NelsonCandela
Enthusiast
Enthusiast

To those of you who are looking for a way of scripting this please review the following information.

Firstly you create a new file called Set-NTP_ESXi-Hosts.csv.
In this file you place all of the ESXi hosts you want this NTP settings to be applied to.

hostname

host-name-001.DOMAIN.TLD

host-name-002.DOMAIN.TLD

host-name-003.DOMAIN.TLD

host-name-004.DOMAIN.TLD

host-name-005.DOMAIN.TLD

...

Secondly you save the following script as Set-NTP.ps1.

Clear-Host

$FilePath = "C:\your\path\to\the\PowerCLI-Script\Set-NTP_ESXi-Hosts.csv"

$FileContent = Import-CSV $FilePath

Foreach ($CsvLine in $FileContent)

{

    $esx = $CsvLine.hostname

    # Add NTP servers separately

    Add-VmHostNtpServer -VMHost $esx -NtpServer ntpserver1.ntpserver.com

    Add-VmHostNtpServer -VMHost $esx -NtpServer ntpserver2.ntpserver.com

    Add-VmHostNtpServer -VMHost $esx -NtpServer ntpserver3.ntpserver.com

    # Set the firewall regulation to allow traffic for NTP lookup

    Get-VMHostFirewallException -VMHost $esx | where {$_.Name -eq "NTP client"} | Set-VMHostFirewallException -Enabled:$true

   

    # Start NTP daemon and make it start automatically when needed

    Get-VmHostService -VMHost $esx | Where-Object {$_.key -eq "ntpd"} | Start-VMHostService

    Get-VmHostService -VMHost $esx | Where-Object {$_.key -eq "ntpd"} | Set-VMHostService -policy "automatic"

}

As you can see from line 11 on you specify the NTP servers to be passed on to each ESXi host given in the .csv file.

The rest is a combination of what's above: read the .csv file, loop through its entries, sets the NTP settings per host for as long as it find lines (and the servers of course) and that's it.

Of course you need to be connected to your vCenter first. This is done by the Connect-VIServer command, as you all know.

Maybe this is of help when you have a list of many servers (as I had, app. 80) where these settings should be applied.