VMware Cloud Community
emcclure
Enthusiast
Enthusiast

What command do I use to change the IP of an ESXi host?

Hello,

I have to re-ip a bunch of ESXi hosts into a different subnet.  I've got a script that will connect to vCenter, let me select the datacenter, select the host, remove the host from vCenter (as the host is also having a fqdn change), disconnect from vCenter, connect directly to the host and then try to reconfigure the host.  I understand it can't be all done in one command, but I'm not sure why it's failing, or what the exact command is.  Is it Set-VMHostNetwork or Set-VMHostNetworkAdapter?  What I have is this:

$ESXIP = Read-Host "Enter new IP address"

Write-Host "Configure new management network"

Get-VMHost "$ESXhost" | Set-VMHostNetworkAdapter -VirtualSwitch "vSwitch0" -PortGroup "Management Network" -IP "$ESXIP" -SubnetMask "255.255.255.0" -ManagementTrafficEnabled:$true -Confirm:$false | out-null

Write-Host "Configure new DNS servers"

Get-VMHost "$ESXhost" | Get-VMHostNetwork | Set-VMHostNetwork -DnsAddress 8.8.8.8,8.8.8.8 -DnsFromDhcp $false

Write-Host "Change VMkernel Gateway"

$net = Get-VMHostNetwork

$net | Set-VMHostNetwork -VMKernelGateway 1.2.3.4 | out-null

But I always seem to get a failure on the setting of the IP, which of course stops it from doing the gateway:

Set-VMHostNetworkAdapter : Parameter set cannot be resolved using the

specified named parameters.

At C:\Users\emcclure\Desktop\GenScripts\GenUpdateHostIPSettings.ps1:39 char:25

+ ... $ESXhost" | Set-VMHostNetworkAdapter -VirtualSwitch "vSwitch0" -PortG ...

+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [Set-VMHostNetworkAdapter],

    ParameterBindingException

    + FullyQualifiedErrorId : AmbiguousParameterSet,VMware.VimAutomation.ViCor

   e.Cmdlets.Commands.Host.SetVMHostNetworkAdapter

I'd figure after this I'd either want to reboot the host or somehow get the network to restart and then add it back to vCenter with the new fqdn.  But how to get the above to work?  Any help appreciated.  Thanks in advance.

0 Kudos
5 Replies
LucD
Leadership
Leadership

You have to feed the object coming from Get-VMHostNetworkAdapter to the Set-VMHostNetworkAdapter cmdlet.

See How To Change vmk0 IP


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

0 Kudos
emcclure
Enthusiast
Enthusiast

Hi LucD,

I just gave that a shot, but for whatever reason it fails for me.  So here's what I'm using for my code:

Get-Datacenter | Format-Table

$Datacenter = Read-Host "Enter datacenter name"

Get-Datacenter $Datacenter | Get-VMHost | sort Name | select Name,ConnectionState,PowerState | Format-Table

$ESXihosts = Get-Datacenter $Datacenter | Get-VMHost

$ESXhost = Read-Host "Enter host to modify"

Sleep 10

$ESXIP = Read-Host "Enter new IP address"

Write-Host "Configure new management network"

Get-VMHost "$ESXhost" | Get-VMHostNetworkAdapter -Name vmk0 | Set-VMHostNetworkAdapter -IP "$ESXIP"

Unfortunately I get this:

Set-VMHostNetworkAdapter : 6/25/2018 8:15:53 AM Set-VMHostNetworkAdapter

        An

error occurred during host configuration. Operation failed, diagnostics

report: Unable to Set: Sysinfo error: Network unreachableSee VMkernel log for

details.

At C:\Users\emcclure\Desktop\GenScripts\GenUpdateHostIPSettings.ps1:39 char:63

+ ... HostNetworkAdapter -Name vmk0 | Set-VMHostNetworkAdapter -IP "$ESXIP"

+                                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [Set-VMHostNetworkAdapter], Pl

   atformConfigFault

    + FullyQualifiedErrorId : Client20_VmHostServiceImpl_SetVMHostNetworkAdapt

   er_ViError,VMware.VimAutomation.ViCore.Cmdlets.Commands.Host.SetVMHostNetw

  orkAdapter

So this is all in the same VLAN, however the VLAN is dual honed (homed), so while one subnet might be say 10.11.x.x, the new one is 10.12.x.x.  If I go onto the host it'll obviously change and work just fine, but for whatever reason it fails when I run the command.  I have a change DNS command in there as well and that works fine, but that's after the IP change fails.

0 Kudos
LucD
Leadership
Leadership

Aren't you cutting of the branch on which you are sitting?

When you change the IP address for the vmk on which the management traffic is running, you will probably loose your connection.

Also, when applying a new IP in a new subnet, you probably have to change the default gateway as well.

Are you using 2 vmk, each in one of the subnets?


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

0 Kudos
emcclure
Enthusiast
Enthusiast

Shouldn't I be able to make the changes and then have the management network restarted, which I know I'll lose my connection, but that's fine as the new IP points to a new DNS name I need to add into vCenter anyway.

I want to change the gateway as well, but it always complains about the IP not being in the same subnet as the gateway, so until I get this IP change to work that won't work.

From what I've found on the interwebs it appears that PowerCLI or ESXi doesn't like having one command do all the changes at once, so I'd need to change the IP, then the gateway, then the DNS, then force the reboot of the services and then add the host back into vCenter with the new name.

I'm also trying to run an esxcli command thru PowerCLI as well, but even though that seems to execute ok I still have the same original IP and not what it's supposed to be.

0 Kudos
LucD
Leadership
Leadership

Since you have 2 vmnics, you should be able to create a 2nd vmk.

I would first create the 2nd, configure it on the 2nd subnet, enable mgmt traffic on there and then remove the 1st one.


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

0 Kudos