PowerCLI

 View Only
Expand all | Collapse all

Powershell script to modify DNS entries for all hosts

  • 1.  Powershell script to modify DNS entries for all hosts

    Posted Jun 02, 2009 06:40 PM

    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?



  • 2.  RE: Powershell script to modify DNS entries for all hosts

    Broadcom Employee
    Posted Jun 02, 2009 06:56 PM

    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



  • 3.  RE: Powershell script to modify DNS entries for all hosts

    Posted Jun 02, 2009 06:57 PM

    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



  • 4.  RE: Powershell script to modify DNS entries for all hosts

    Broadcom Employee
    Posted Jun 02, 2009 07:17 PM

    Saying that, I get the same :smileyhappy: 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



  • 5.  RE: Powershell script to modify DNS entries for all hosts
    Best Answer

    Broadcom Employee
    Posted Jun 02, 2009 08:42 PM

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

     $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



  • 6.  RE: Powershell script to modify DNS entries for all hosts

    Posted Jun 02, 2009 08:47 PM

    That worked!!!! Awesome!

    Thanks a bunch!



  • 7.  RE: Powershell script to modify DNS entries for all hosts

    Posted Jun 30, 2010 04:09 PM

    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.



  • 8.  RE: Powershell script to modify DNS entries for all hosts

    Posted Jun 30, 2010 05:20 PM

    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



  • 9.  RE: Powershell script to modify DNS entries for all hosts

    Posted Jun 30, 2010 06:38 PM

    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?



  • 10.  RE: Powershell script to modify DNS entries for all hosts

    Posted Jun 30, 2010 06:59 PM

    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



  • 11.  RE: Powershell script to modify DNS entries for all hosts

    Posted Jul 01, 2010 12:55 PM

    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"



  • 12.  RE: Powershell script to modify DNS entries for all hosts

    Posted Jul 01, 2010 02:54 PM

    Sweet!!! That worked like a charm!

    Thank you very much!



  • 13.  RE: Powershell script to modify DNS entries for all hosts

    Posted Feb 17, 2011 07:01 PM

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



  • 14.  RE: Powershell script to modify DNS entries for all hosts

    Posted Feb 17, 2011 11:50 PM

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