PowerCLIvHerseyVMware

Setting ESXi DNS and NTP using PowerCLI

This is the second post in a series of PowerCLI scripts I use when doing new vSphere implementations. The first script created a new Data Center in vCenter Inventory and added a list of ESXi hosts to the newly created Data Center.

After connecting to vCenter using Connect-VIServer run this PowerCLI script to configure the primary and alternate DNS servers, the domain name, and the NTP settings of each ESXi host in the vCenter Inventory. The script first prompts for the DNS, domain name, and NTP server information. Get-VMHost retrieves a list of the ESXi host in inventory and then applies the settings to each host. Once the configurations are applied the script enables the NTP Client and restarts NTP so that the hosts will receive time from the configured NTP sources.

# PowerCLI Script to Configure DNS and NTP on ESXi Hosts
# PowerCLI Session must be connected to vCenter Server using Connect-VIServer

# Prompt for Primary and Alternate DNS Servers
$dnspri = read-host "Enter Primary DNS"
$dnsalt = read-host "Enter Alternate DNS"

# Prompt for Domain
$domainname = read-host "Enter Domain Name"

#Prompt for NTP Servers
$ntpone = read-host "Enter NTP Server One"
$ntptwo = read-host "Enter NTP Server Two"

$esxHosts = get-VMHost

foreach ($esx in $esxHosts) {

   Write-Host "Configuring DNS and Domain Name on $esx" -ForegroundColor Green
   Get-VMHostNetwork -VMHost $esx | Set-VMHostNetwork -DomainName $domainname -DNSAddress $dnspri , $dnsalt -Confirm:$false

   
   Write-Host "Configuring NTP Servers on $esx" -ForegroundColor Green
   Add-VMHostNTPServer -NtpServer $ntpone , $ntptwo -VMHost $esx -Confirm:$false

 
   Write-Host "Configuring NTP Client Policy on $esx" -ForegroundColor Green
   Get-VMHostService -VMHost $esx | where{$_.Key -eq "ntpd"} | Set-VMHostService -policy "on" -Confirm:$false

   Write-Host "Restarting NTP Client on $esx" -ForegroundColor Green
   Get-VMHostService -VMHost $esx | where{$_.Key -eq "ntpd"} | Restart-VMHostService -Confirm:$false

}
Write-Host "Done!" -ForegroundColor Green

The script requires that two DNS and two NTP servers are configured but could easily be modified to only require one (not really a best practice but sometimes that is just the way it is). It will produce an error if only one DNS or NTP server is entered, there are no checks for this but that could be added.

Much faster than accessing the configuration of each host using the vSphere Client and the script also ensures these configurations are consistent across all hosts in inventory.

Hope you find it useful. Comments or recommendations for improvement are always welcome.

7 thoughts on “Setting ESXi DNS and NTP using PowerCLI

  • Hi Hersey,

    Thanks for the excellent script. I often find small errors creeping in when my clients decide to setup the NTP and DNS settings manually. The NTP settings may not be immediately obvious that it’s incorrect and will sometimes only reveal itself as an issue when the clock drift is beyond 5 minutes.

    Cheers

    Graham

    Reply
  • Mike Pagán

    As a coding exercise I was crudely working my way to what you created (setting NTP and DNS on hosts). I like the layout and user feedback you put in there. Thanks for sharing your script.

    Reply
  • Does the script work on ESXi 6.x or would it require modification?

    Reply
  • I’d like to check if my DNS configuration on Vcenter is perfectly done. I’d like to check DNS from IP as well as from Configuration settings. Could you please help me on this. many Thanks.

    Regards,
    Abhishek Singh

    Reply
  • Hello Hersey,

    Is there any way to update the IP address and hostnames for ESXi servers using powercli.

    I am Installing the ESXi servers using AutoDeploy .Post deployment I would like to change the IP address and hostnames.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

ten − 4 =