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?