PowerCLI Series – sample useful scripts – configure DNS, domain, and NTP

Hello again,

Don’t you love time off (and wife not around)?  I can work on what I want.  So very productive on this PowerCLI stuff, and I will do a vC upgrade blog soon too.  But back to this.  In part 1 we got things working, and in part 2 we saw a sample script that does something useful (turns off SSH), but can be changed to something else as well.  In this article we are going to look at a script that does a little more!  It will configure DNS server, the DNS domain, and NTP, plus start NTP.

The command to execute it is below.

.\SetDNSDomainNTP.ps1

You will see something like below.

dns

Depending on the number of your hosts it could be a long list.  It was for my four hosts.  But if you check your hosts, you will see the new DNS server, DNS domain, NTP servers, and running NTP.  And when you buy more hosts, and run the script again, the exact same config will happen.  Nice.

I found this script on the internet (here), and have modified it.  The original location for it is in the remarks of the script as well.  This is one of the reasons I like PowerCLI – there is so much to find on the Internet that might do your work for you!

Lets look at the script.


# PowerCLI Script to Configure DNS and NTP on ESXi Hosts
# PowerCLI Session must be connected to vCenter Server using Connect-VIServer
# thanks - http://www.vhersey.com/2013/10/setting-esxi-dns-and-ntp-using-powercli/
# 12/23/13 - M. White - remove dnsalt and add ntp.

# Prompt for Primary and Alternate DNS Servers
$dnspri = read-host "Enter Primary 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"
$ntpthree = read-host "Enter NTP Server Three"

$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 -Confirm:$false

 Write-Host "Configuring NTP Servers on $esx" -ForegroundColor Green
 Add-VMHostNTPServer -NtpServer $ntpone , $ntptwo , $ntpthree -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

This is a fancier script that the previous ones.  In the script I found on the internet it only did two NTP servers, and it did two DNS servers.  I have three NTP – like everyone should, and I have right now only one DNS.  So small tweaks.  So small tweaks vs. coding from the start.

In lines 7 – 15 we see the read-host command where questions are asked.  Lines 17 through 19 setup and do the loop which is where the payload is held.  In lines 21 – 31 it is the payload.  In this case it is doing the config of the things we are changing and where appropriate starting things.  I like how in this script it provides you with information on what is happening so you can follow along.  Very handy.

What did I change?  On line 22, I removed the item dnsalt and the comma from in front of it.  Than I removed line 8 which was a prompt for the alternative DNS.  I added the prompt for the third NTP server in line 15.  Than in line 17 I added a ,$ntpthree to be the third NTP server added.  And it worked.  This is important to understand it was not a big deal, but it meant a very nice script was customized easily to work in my environment.  Again, that is important about PowerCLI – lots of almost right stuff you can use.

You can copy and paste from the script source code above if you like.  Check out part 1 – Getting Started, or part 2 – stop SSH if you need to get started with PowerCLI or check out a simple script.

I really like what comes next.  I hope you do too!

Michael

Tagged with: , ,
Posted in How To
One comment on “PowerCLI Series – sample useful scripts – configure DNS, domain, and NTP
  1. Hersey says:

    Great mods to the script! Glad you found it useful and thanks for the link. Look forward to reading more of your post.

    Hersey

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: