Hi Team,
we have 250 + esxi servers and we have to change all Esxi Ips,subnetmask, gataway and vlan at the same time. When I try executing , I am either able to change IP or VLan at the same and it gets disconnected. Is there any way to change the all parameters at the same time. I am trying to change but as soon I change vlan or Ip it gets disconnected. Please find the below script.
Remove-Variable * -ErrorAction SilentlyContinue
Import-Module -Name VMware.VimAutomation.Core
$current_Esxi_Ip = '1.1.1.1' # current ESXi ip
$new_Esxi_Ip = '1.1.1.11' # New Esxi Ip after changing
$current_gateway = '1.1.1.1' # Current_gateway
$newgateway ='1.1.1.2' # New Gateway
$vlanid ='1000' # New VLan Id
$dns1 = '1.1.1.150' # New DNS Ip1
$dns2 = '1.1.1.151' # New DNS Ip2
$domain_Name = 'dname.com' # New DNS name
$domain_Search = 'dname.com' # New DNS domain search
$New_Esxi_Host_name = 'esxi100' # New Esxi hostname
$subnetmask ='255.255.0.0' # New Subnet Mask
$esxi_password = 'abcdef'
Connect-VIServer $current_Esxi_Ip -Username root -Password $esxi_password -ErrorAction SilentlyContinue
#change Esxi IP and subnet mask
$change_EsxiIp_and_subnetmask = Get-VMHostNetworkAdapter -VMHost $current_Esxi_Ip | ? {$_.ip -like “1.1.1.*”}| Set-VMHostNetworkAdapter -IP $new_Esxi_Ip -SubnetMask $subnetmask -ManagementTrafficEnabled $True -verbose
#change VLan ID
$change_management_vlanid = Get-VMHost -name $current_Esxi_Ip | Get-VirtualPortGroup -Name "Management Network" | Set-VirtualPortGroup -Name "Management Network" -vlanid $vlanid -verbose
#change dnsIPs, Domain name , Domain name search, Gateway
Get-VMHost -name $current_Esxi_Ip | Get-VMHostNetwork | Set-VMHostNetwork -DnsAddress $dns1,$dns2 -DomainName $domain_Name -HostName $New_Esxi_Host_name -SearchDomain $domain_Search -VMKernelGateway $newgateway -verbose
#restart management
Get-VMHostService -VMHost $current_Esxi_Ip | ? {$_.key -eq “vmware-vpxa”} | Restart-VMHostService -verbose
Tried via Escxli powercli as well but I am able to change either vlan or Ip address at the same time.
#################### changing the IP address , gateway, subnet mask and vlan via esxcli via powercli.
#Esxcli
$esx = Get-VMHost -Name $current_Esxi_Ip
$esxcli = Get-EsxCli -VMHost $esx
$esxcli.network.ip.interface.ipv4.set($newgateway,'vmk0',$new_Esxi_Ip,$subnetmask, $null,'static')
$esxcli.network.vswitch.standard.portgroup.set('Management Network', $vlanid)
Message was edited by: DEEPAK antharvedi I have tried ESXCLI commands from powercli to change IPAddress, Subnet, Gateway and VLan.
i assume that after you change the ip you will loose connection to the esxi and have to reconnect to the new ip to go on with the script
do you get an error message ?
Hi
yes. It disconnects as soon as we change IP address. Currently the Esxi is on Trunk port i.e different vlan and IP address i.e (172.17.xx.xx on vlan 1000). we might be getting different vlan and different network (10.10.xx.xx on vlan 700).
The current script I am able to execute commands to change IP address but we get trunk port we need to mention the trunk port new vlan ID as well. So I am able to change either IP address or VLan only. Is there any way Where I can change IP address and Vlan as the same time ?
1. current scenario Trunk port
Vlan : 1000
Ipaddress : 172.17.xx.xx
2. Required network range Trunk port
Required scenario
VLan : 700
Ipaddress: 10.10.xx.xx
i think your only option is to add a secondary vmkernel-port for management with new ip and vlan.
be sure to put host in maintenance because you have to remove the host from vcenter and re-register with the new ip
Just thinking out loud here, based on what bewe said. I think the key would be to disconnect the host from vcenter first, then run the script with the modifications on a vSwitch and then connect the host to vcenter again with the new IP and VLAN. I found a script which does just that, it might be a little old, but I think with some modification it might do the trick. At least it does what I was thinking,
You can probably make modification to make it work in your environment.
hope this helps! Good luck