PowerCLI Script to Configure DNS and NTP on ESXi Hosts – VirtuallyThatGuy

This is a quick blog post on how to update DNS and NTP settings via powercli. You can also check my other post on how to do it via host profile

$vCenters = “lab-vcenter01.lab.local”, “lab-vcenter02.lab.local”

 

# PowerCLI Script to Configure DNS and NTP on ESXi Hosts

# PowerCLI Session must be connected to vCenter Server using Connect-VIServer

 

Connect-VIServer​​ $vCenters​​ -User​​ rboadi@lab.local​​ -Password​​ StandUpIfYouHateTottenham ​​​​ -ErrorAction​​ SilentlyContinue​​ 

 

# Prompt for Primary and Alternate DNS Servers

$dnspri​​ =​​ read-host​​ “Enter Primary DNS”

$dnsalt​​ =​​ read-host​​ “Enter Secondary DNS”

 

# Prompt for Domain

$domainname​​ =​​ read-host​​ “Enter Domain Name”

 

#Prompt for NTP Servers

$ntpone​​ =​​ read-host​​ “Enter NTP Server 1”

$ntptwo​​ =​​ read-host​​ “Enter NTP Server 2”

 

$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

 

​​ 

 

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

Connect-VIServer $vCenters -User rboadi@lab.local -Password StandUpIfYouHateTottenham  -ErrorAction SilentlyContinue 

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

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

#Prompt for NTP Servers
$ntpone = read-host “Enter NTP Server 1”
$ntptwo = read-host “Enter NTP Server 2”

$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

 

You May Also Like

About the Author: VirtuallyThatGuy

Leave a Reply

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