I needed to change the DNS setup in our vSphere environment. Instead of doing this by hand on every host I decided to create a script. First I needed a script to add the A and PTR records to the Windows DNS servers. I remembered a post by the scripting guys so I took their function and added it to my script. The final step is to change de vSphere host DNS configuration. This one is easy with PowerCLI and a simle for loop.
Warning! If you are using vSphere 4.1 and the vSphere hosts are joined to a Windows domain. You are not able to change the DNS settings!
From the Hey Scripting Guy post I quote the following about the new-dnsrecord function:
I’ve written various scripts in the past to work with individual record types, and I’ve found that each class has slightly different syntax and requirements. This makes life awkward when you want to start automating this process, because you have to have a different script or function for each record type. I decided I wanted a universal script for creating records so that I could create multiple records at the same time from minimal information. The following script shows the function that I came up with to create A, PTR, MX, and CNAME records—these being the most common ones I have to deal with. We will be using the MicrosoftDNS_ResourceRecord class with varying inputs.
I have combined the new-dnsrecord function with some PowerCLI code to accomplish my goal of migrating the DNS settings of all the vSphere hosts and to add all the hosts to the DNS servers. I did this task by running the following script:
$pg = "Service Console" $fzone = "ict-freak.local" $rlzone = "123.168.192.in-addr.arpa" $dc = "dc01.ict-freak.local" $dns1 = "192.168.123.1" $dns2 = "192.168.123.2" $domain = "ict-freak.local" $domain2 = "ict-freak.loc" # # Source: # http://blogs.technet.com/b/heyscriptingguy/archive/2010/09/13/manage-dns-in-a-windows-environment-by-using-powershell.aspx # function new-dnsrecord { param( [string]$server, [string]$fzone, [string]$rzone, [string]$computer, [string]$address, [string]$alias, [string]$maildomain, [int]$priority, [switch]$arec, [switch]$ptr, [switch]$cname, [switch]$mx ) ## check DNS server contactable if (-not (Test-Connection -ComputerName $server)){Throw "DNS server not found"} ## split the server fqdn and address $srvr = $server -split "\." $addr = $address -split "\." $rec = [WmiClass]"\\$($srvr[0])\root\MicrosoftDNS:MicrosoftDNS_ResourceRecord" ## ## create records ## ## A if ($arec){ $text = "$computer IN A $address" $rec.CreateInstanceFromTextRepresentation($server, $fzone, $text) } ## CNAME if ($cname){ $text = "$alias IN CNAME $computer" $rec.CreateInstanceFromTextRepresentation($server, $fzone, $text) } ## PTR if ($ptr){ $text = "$($addr[3]).$rzone IN PTR $computer" $rec.CreateInstanceFromTextRepresentation($server, $rzone, $text) } ## MX if ($mx){ $text = "$maildomain IN MX $priority $computer" $rec.CreateInstanceFromTextRepresentation($server, $fzone, $text) } } foreach($esx in (Get-Cluster | Get-VMHost | Sort Name)){ $ip = ($esx |Get-vmhostnetworkadapter | Where-Object {$_.PortGroupName -eq $pg}).IP $esxHost = $esx.name.Split(".")[0] $fqdn = "$esxHost.$domain" new-dnsrecord -server $dc -fzone $fzone -rzone $rlzone -computer $fqdn -address $ip -arec -ptr $esx | Get-VMHostNetwork | Set-VMHostNetwork -Domain $domain -SearchDomain $domain,$domain2 -Dnsaddress $dns1,$dns2 -confirm:$false }
Annotations:
The $domain2 parameter can be used if you need to add a second “Search Domain”. You can remove this parameter if you don’t need to add an extra search domain.
Before the script can continue we need to get the IP address of the Service Console portgroup:
$ip = ($esx |Get-vmhostnetworkadapter | Where-Object {$_.PortGroupName -eq $pg}).IP
To get the vSphere hostname instead of the full qualified domain name you can use the Split method and select the first [0] of the array.
$esxHost = $esx.name.Split(".")[0]
The $fqdn parameter will join the vSphere Hostname with the new domain.
$fqdn = "$esxHost.$domain"
The following line will generate the DNS A and PTR records:
new-dnsrecord -server $dc -fzone $fzone -rzone $rlzone -computer $fqdn -address $ip -arec -ptr
The last step is to change the vSphere host configuration:
$esx | Get-VMHostNetwork | Set-VMHostNetwork -Domain $domain -SearchDomain $domain,$domain2 -Dnsaddress $dns1,$dns2 -confirm:$false
When you change the domain on the vSphere host. You need to restart the vSphere host.
If you’re running ESX4.0 (Classic), You can also run /etc/init.d/network restart to activate the new configuration instead of a reboot.
I want to thank Ed Wilson and Craig Liebendorfer aka the Scripting Guys for writing the new-dnsrecord function and their great blog!
Source: | http://blogs.technet.com/b/heyscriptingguy/archive/2010/09/13/manage-dns-in-a-windows-environment-by-using-powershell.aspx |
Many thanks for the info 🙂