Odurasler2
Enthusiast
Enthusiast

ESXi Hostname Change when "DnsFromDHCP" is enabled

Jump to solution

I have a generic script below that changes the IP from DHCP to Static. Because it's on the same subnet, only the last octet changes. All DNS, gateway, DNS suffixes are all the same. However, when I try to execute the hostname change, I'm unable to do so because of the excerpt:  "If 'DnsFromDhcp' is enabled DomainName and HostName cannot be explicitly set."

So my workaround has been to execute a reboot of the hosts before I execute the host name change.  Is there a better way of executing this script without a reboot, or perhaps, a restart of the network so that the "static" can take place? I have seen that once the IP is set to static, it automatically changes the DnsFromDhcp to FALSE which is required to explicitly change the hostname.

Here's my script:

$Servers = import-csv C:\esxi.csv

foreach($Server in $Servers)

{

    connect-viserver -server $Server.oldIP -protocol https -user root -password $Server.HostPassword

   

    $HostNetAdapter = get-vmhostnetworkadapter -VMKernel -Name vmk0

    $HostNet = get-vmhostnetwork

  

    set-vmhostnetworkadapter -IP $Server.NewIP -SubnetMask $Server.Mask -VirtualNic $HostNetAdapter #?perhaps, perform a restart of network here?

    set-vmhostnetwork -Network $HostNet -Hostname $Server.HostName

   

Disconnect-viserver -Server * -Force -Confirm:$false

}

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

What happens when you first do

Get-VMHost -Name esx1* | Get-VMHostNetwork | Set-VMHostNetwork -DnsFromDhcp:$false -Confirm:$false

and then change the HostName and Domainname


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

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership

What happens when you first do

Get-VMHost -Name esx1* | Get-VMHostNetwork | Set-VMHostNetwork -DnsFromDhcp:$false -Confirm:$false

and then change the HostName and Domainname


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

View solution in original post

0 Kudos
Odurasler2
Enthusiast
Enthusiast

that works!

0 Kudos