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.