angoletti1
Contributor
Contributor

HowTo change DNS servers on ESX host?

Hi,

is there a way to change the DNS server on every host via the powershell?

Thanks

Chris

0 Kudos
5 Replies
LucD
Leadership
Leadership

Sure, something like this should do the trick


$dnsServers = ("192.168.111.3","192.168.111.4")

Get-VMHost | Get-View | %{
   $ns = Get-View -Id $_.configManager.networkSystem
   $dns = $ns.networkConfig.dnsConfig
 
    $dns.Address = @()
    foreach($server in $dnsServers) {
      $dns.Address += $server
  }
  $ns.UpdateDnsConfig($dns)
}

It looks as if an ESX server accepts only 2 DNS servers.

Message was edited by: LucD

Corrected error in the script.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
angoletti1
Contributor
Contributor

Hi,

thanks for your answer. I still have a few problems with it. Can you look in the attachment and tell me what's wrong?

0 Kudos
LucD
Leadership
Leadership

There was a dot (.) missing in the foreach line.

foreach($server in $dns.Servers) {

Sorry about that.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
MoRobin
Contributor
Contributor

Is there a way to change just the 2. DNS Server?

And also check if the first has the same IP that i want to fill into the 2.?

And when that is true it should change first an 2. IP.

Is that possible?

0 Kudos
LucD
Leadership
Leadership

You could do something like this

$esxName = <esx-name>
$new2ndIP = "192.168.111.1"
$net = Get-VMHost $esxName | Get-VMHostNetwork
$dns = $net.DnsAddress
if($dns[0] -eq $new2ndIP){
	Write-Host "First DNS IP address is the same"
	 $new2ndIP = Read-Host "Enter new IP address"
}
$dns[1] = $new2ndIP
$net | Set-VMHostNetwork -DnsAddress $dns

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos