Automation

 View Only
  • 1.  Unable to fix issues with my script to set DNS on ESXi severs

    Posted Aug 03, 2022 10:05 AM

    Hi All, 

    I am trying to execute the following :

    ========
    function setDNSonESXi {
    try{
    $esxiName = Read-Host "Enter ESXi Name"
    $dnsServers = Read-Host "Enter DNS Servers as comma separated values  "
    Get-VMHost -Name $esxiName | %{
    $esxcli = Get-EsxCli -VMHost $_ -V2
    $esxcli.network.ip.dns.server.list.Invoke() | select -ExpandProperty DNSServers | %{
    $sOldDns = @{
    server = $_
    }
    $esxcli.network.ip.dns.server.remove.Invoke($sOldDns)
    }
    $dnsServers | %{
    $sDns = @{
    server = $_
    }
    $esxcli.network.ip.dns.server.add.Invoke($sDns)
    }
    }
    }
    catch{
    Write-Host -foregroundColor Red -backgroundColor Black "`nCould not add DNS to the server"
    throw $_.Exception.Message
    exit
    }
    }
    ========

    However, when I execute this, it asks me for the input, and if I give multiple IP addresses, it gives the following error(IPs masked) : 
    =====
    Could not add DNS to the server
    Message: EsxCLI.CLIFault.summary;
    InnerText: The specified address 'x.x.x.x,y.y.y.y' is not recognised as a valid IP addressEsxCLI.CLIFault.summary
    At E:\Test_setDNSUtilityForESXiHosts.ps1:142 char:3
    + throw $_.Exception.Message
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : OperationStopped: (Message: EsxCLI...LIFault.summary:String) [], RuntimeException
    + FullyQualifiedErrorId : Message: EsxCLI.CLIFault.summary;
    InnerText: The specified address 'x.x.x.x,y.y.y.y' is not recognised as a valid IP addressEsxCLI.CLIFault.summary
    ================


    and when I give only 1 IP address, it throws the following error : 

    ================

    select : Property "DNSServers" cannot be found.
    At E:\Test_setDNSUtilityForESXiHosts.ps1:126 char:50
    + ... .ip.dns.server.list.Invoke() | select -ExpandProperty DNSServers | %{
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:PSObject) [Select-Object], PSArgumentException
    + FullyQualifiedErrorId : ExpandPropertyNotFound,Microsoft.PowerShell.Commands.SelectObjectCommand

    ==============

    I want to write this script to accept input for ESXi host name and set DNS for it, and also to accept Cluster name and set DNS for all the ESXi in the cluster. However, I am not able to progress because of these errors. 

    I kindly request you to help me fix the issue in hand. 

    Thank you in advance. 

     

     

     



  • 2.  RE: Unable to fix issues with my script to set DNS on ESXi severs

    Posted Aug 03, 2022 11:10 AM

    You have to convert $dnsServers to an array.
    Add 1 line to split the input string

            $dnsServers = Read-Host "Enter DNS Servers as comma separated values  "
            $dnsServers = $dnsServers.Split(',')
    

     



  • 3.  RE: Unable to fix issues with my script to set DNS on ESXi severs

    Posted Aug 03, 2022 01:20 PM

    If you are connected through vCenter you can try the below for a single host.

    $esx=Read-Host "Enter ESXi Name"
    $priDNS=Read-Host "Enter Primary DNS"
    $secDNS=Read-Host "Enter Secondary DNS"
    $host = Get-VMHost $esx
    $dnsaddress = @()
    $dnsaddress += "$priDNS"
    $dnsaddress += "$secDNS"
    Get-VMHost $host | Get-VMHostNetwork | Set-VMHostNetwork -DnsAddress $dnsaddress

    and below for all the hosts on vCenter

    $hosts = Get-VMHost
    $dnsaddress = @()
    $dnsaddress += "x.x.x.x"
    $dnsaddress += "x.x.x.x"
    Foreach ($host in $hosts) {
    Get-VMHost $host | Get-VMHostNetwork | Set-VMHostNetwork -DnsAddress $dnsaddress}

    also for a specific Cluster alone

    $hosts = get-cluster "Cluster name" | Get-VMHost
    $dnsaddress = @()
    $dnsaddress += "x.x.x.x"
    $dnsaddress += "x.x.x.x"
    Foreach ($host in $hosts) {
    Get-VMHost $host | Get-VMHostNetwork | Set-VMHostNetwork -DnsAddress $dnsaddress}



  • 4.  RE: Unable to fix issues with my script to set DNS on ESXi severs

    Posted Aug 04, 2022 03:42 AM

    Thank you for the help. 



  • 5.  RE: Unable to fix issues with my script to set DNS on ESXi severs

    Posted Aug 04, 2022 03:41 AM

    Thanks a lot LucD, it worked as expected, a little tweak to the code above and I was also able to get it working for the cluster based selection. 

    Regards,