VMware Cloud Community
atreyash
Contributor
Contributor

Unable to fix issues with my script to set DNS on ESXi severs

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. 

 

 

 

0 Kudos
4 Replies
LucD
Leadership
Leadership

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(',')

 


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

Godwin_Christop
Enthusiast
Enthusiast

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}

✌️

0 Kudos
yashatrevm
Enthusiast
Enthusiast

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,

 

 

0 Kudos
yashatrevm
Enthusiast
Enthusiast

Thank you for the help. 

0 Kudos