PowerCLI

  • 1.  PowerCLI Script to Change DNS Servers and Domain

    Posted Jan 10, 2011 11:15 PM

    Hello

    Very new to PowerCLI, I am trying to create a script that will change the DNS Servers and Domain name of my ESXi servers. I have found I need to update the Vmware.Vim.HostDnsConfig to update these settings. I have created the following script and receive the following error.

    $config = New-Object VMware.Vim.HostDnsConfig
    $config.domainName = "new.domain.com"
    $config.address = New-Object System.String[] (2)
    $config.address[0] = "10.69.69.80"
    $config.address[1] = "10.69.70.80"

    $_this = Get-View -Id 'HostNetworkSystem-networkSystem'

    $_this.UpdateDnsConfig($config)

    You cannot call a method on a null-valued expression.

    At line:1 char:23

    + $_this.UpdateDnsConfig <<<< ($config)

        + CategoryInfo          : InvalidOperation: (UpdateDnsConfig:String) [], RuntimeException

        + FullyQualifiedErrorId : InvokeMethodOnNull

    I guessed from the error message that I need a value in the hostname, only I don't need to change the hostname. So I create a variable to pull the hostname and place it in the $config.Hostname line but still received an error because the variable is pulling more information that I need.

    $hostname = Get-VMHost | Select Name

    $config.Hostname=$hostname

    When I insert the above code I an error.

    Exception calling "UpdateDnsConfig" with "1" argument(s): "An error occurred during host configuration."
    At line:1 char:23
    + $_this.UpdateDnsConfig <<<< ($config)
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : DotNetMethodException

    When I look at the $config I see the following.

    Dhcp             : False
    VirtualNicDevice :
    HostName         : @{Name=ESXI01}
    DomainName       : new.domain.com
    Address          : {10.69.69.80, 10.69.70.80}
    SearchDomain     :
    DynamicType      :
    DynamicProperty  :

    Of course the @{Name=ESXI01} will not work. Can anyone explain how to pull a hostname in a variable and pass it to the Config.Hostname?



  • 2.  RE: PowerCLI Script to Change DNS Servers and Domain
    Best Answer

    Posted Jan 10, 2011 11:48 PM

    You should be able to do this easily by using the "set-vmhostnetwork" command.

    I don't think you can just write to VMWare.Vim.HostDnsConfig.



  • 3.  RE: PowerCLI Script to Change DNS Servers and Domain

    Posted Jan 11, 2011 02:56 AM

    Thank You, I looked up this command and with a little trial and error found the correct syntax.

    Get-VMHost | Get-VMHostNetwork | Set-VMHostNetwork -DomainName new.domain.com



  • 4.  RE: PowerCLI Script to Change DNS Servers and Domain

    Posted Jan 21, 2025 11:38 AM

    Please try it:

    $VMhosts = Get-VMhost #Prompt for Primary and Alternate DNS Servers
    $dnspri = read-host "Enter Primary DNS";
    $dnsalt = read-host "Enter Alternate 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"
     
    $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 , $dnsalt -Confirm:$false
     
        
       #Write-Host "Configuring NTP Servers on $esx" -ForegroundColor Green
       #Add-VMHostNTPServer -NtpServer $ntpone , $ntptwo -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