VMware Cloud Community
bigvee
Enthusiast
Enthusiast
Jump to solution

Powershell script to modify DNS entries for all hosts

Ive tried many that have been posted around the web and none work... Anyone have a script to change the DNS entries on all hosts?

0 Kudos
1 Solution

Accepted Solutions
alanrenouf
VMware Employee
VMware Employee
Jump to solution

Ok, changed a few bits my end, it wasnt working but now is... try this (50% confident Smiley Happy

 $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)
 }
 




If you found this information useful, please consider awarding points for Correct or Helpful.

Alan Renouf

http://virtu-al.net

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com

View solution in original post

0 Kudos
13 Replies
alanrenouf
VMware Employee
VMware Employee
Jump to solution

Have you tried LucD's answer on a recent post... http://communities.vmware.com/message/1049343#1049343

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




If you found this information useful, please consider awarding points for Correct or Helpful.

Alan Renouf

http://virtu-al.net

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
0 Kudos
bigvee
Enthusiast
Enthusiast
Jump to solution

I have and it doesnt seem to work.....

Property 'Address' cannot be found on this object; make sure it exists and is s

ettable.

At line:6 char:12

+ $dns.A <<<< ddress += $server

Property 'searchDomain' cannot be found on this object; make sure it exists and

is settable.

At line:8 char:8

0 Kudos
alanrenouf
VMware Employee
VMware Employee
Jump to solution

Saying that, I get the same Smiley Happy Let me have a look !



If you found this information useful, please consider awarding points for Correct or Helpful.

Alan Renouf

http://virtu-al.net

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
0 Kudos
alanrenouf
VMware Employee
VMware Employee
Jump to solution

Ok, changed a few bits my end, it wasnt working but now is... try this (50% confident Smiley Happy

 $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)
 }
 




If you found this information useful, please consider awarding points for Correct or Helpful.

Alan Renouf

http://virtu-al.net

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
0 Kudos
bigvee
Enthusiast
Enthusiast
Jump to solution

That worked!!!! Awesome!

Thanks a bunch!

0 Kudos
msuvanto
Contributor
Contributor
Jump to solution

Could I limit the script by datacenters? or even better exclude on eor two datacenters?

Here is my problem.. I need to change the primary and secondary DNS server on all local ESX servers but leave branch office ESX servers alone.

Mostly they slide nicely into different datacenters but in some cases they are mixed.

local servers are all on 192.168.8-15.x subnets... and branch servers are different so I do ot wish to touch their DNS servers.

I figure I can go in manually and change the datacenters that have mixed content.. only talking about 20-30 hosts but the rest would be nice to have a script for about 170 of them.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can easily limit the returned hosts to a specific datacenter.

Change the start of the loop to

Get-VMHost -Location (Get-Datacenter -Name $dcName) | Get-View | %{
...

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
msuvanto
Contributor
Contributor
Jump to solution

This being my first excursion into the PowerCLI that I just now

installed... do I just copy this script in? or make it into a file of

some sorts and upload it somewhere and run it?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Store the script in a .ps1 file.

Start the PowerCLI prompt.

Connect to the vCenter or an ESX(i) host.

Connect-VIServer -Server name....

At the prompt type the name of the script to execute

.\ESX-change-DNS.ps1

Jonathan wrote a very good PowerCLI intro

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
esarakaitis
Enthusiast
Enthusiast
Jump to solution

this can also be done as a oneliner!

get-cluster "Test Cluster" | get-vmhost | get-vmhostnetwork | set-vmhostnetwork -dnsaddress "192.168.1.1","192.168.1.2"

0 Kudos
msuvanto
Contributor
Contributor
Jump to solution

Sweet!!! That worked like a charm!

Thank you very much!

0 Kudos
NYSDHCR
Enthusiast
Enthusiast
Jump to solution

When this runs, will I be prompted to enter the datacenter name?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

No, you have to store the datacentername in the variable $dcName


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

0 Kudos