VMware Cloud Community
TakoRoni
Enthusiast
Enthusiast
Jump to solution

script to change vlanid ip and gateway for ESXi

hi

we are changing the Vlan for the ESXi

and get new address and default gateway  for the  ESXi

i can change all of it from putty with dcui

the change take affect only after we go back pessing ESC. ( change IP DG and VLAN )

so the esx is in the new network with new ip and new default gateway in 1 move

but i have 200 esx and i need to automate the procesdure

i can not find a command the can do it in 1 move

if i change the vlan i lose network because the ip and DG are still the old one

if i change the ip and DG i lose network because ti am on the old vlan

i appropriate a solution

Roni

Reply
0 Kudos
1 Solution

Accepted Solutions
TakoRoni
Enthusiast
Enthusiast
Jump to solution

here is the script

now it works Smiley Happy

only thing that remain is to add a CSV file and run it with it

******************************************************************

$esxName = 'esx01'

$tgtPg = 'Management Network'  #portGroup

$vmkName = 'vmk0'

$newVLAN = '111'

$newDG = '1.1.1.1'

$newip = '1.1.1.10'

$stackName = 'defaultTcpipStack'

$esx = Get-VMHost -Name $esxName

$netMgr = Get-View -Id $esx.ExtensionData.ConfigManager.NetworkSystem

$config = New-Object VMware.Vim.HostNetworkConfig

#change Vlan ID

$pg = $netMgr.NetworkConfig.Portgroup | where{$_.Spec.Name -eq $tgtPG}

$pg.ChangeOperation = [VMware.Vim.HostConfigChangeOperation]::edit

$pg.Spec.VlanId = $newVLAN

$config.Portgroup += $pg

#change Defult Gateway

#$netMgr = Get-View -Id $esx.ExtensionData.ConfigManager.NetworkSystem

$stack = $esx.ExtensionData.Config.Network.NetStackInstance | where{$_.Key -eq $stackName}

$spec = New-Object VMware.Vim.HostNetworkConfigNetStackSpec

$spec.Operation = [VMware.Vim.ConfigSpecOperation]::edit

$spec.NetStackInstance = $stack

$spec.NetStackInstance.RouteTableConfig = New-Object VMware.Vim.HostIpRouteTableConfig

$route = New-Object VMware.Vim.HostIpRouteConfig

$route.defaultGateway = $newDG

$route.gatewayDevice = $vmkName

$stack.IpRouteConfig.DefaultGateway = $newDG

$spec.NetStackInstance.ipRouteConfig = $route

#change ip address to ESXi

$vmk = $netmgr.NetworkConfig.Vnic | where{$_.Device -eq $vmkName}

$vmk.ChangeOperation =[VMware.Vim.ConfigSpecOperation]::edit

$vmk.Spec.Ip.IpAddress= $newip

# close and commit changes

$config.NetStackSpec += $spec

$config.Vnic += $vmk

$netMgr.UpdateNetworkConfig($config,[VMware.Vim.HostConfigChangeMode]::modify)

*********************************************************

View solution in original post

Reply
0 Kudos
20 Replies
LucD
Leadership
Leadership
Jump to solution

Since you are referring to the DCUI, I assume this is on a VSS?


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

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

If you are talking about a VSS, you could do something like this.

Afaik, you have to use the API for this, afaik there is no cmdlet for it.
When this code works for you, it should be rather simple to place the code in a loop and do it for all your ESXi nodes.

$esxName = 'MyEsx'

$tgtPg = 'vssPg1'

$vmkName = 'vmk0'

$newVLAN = 111

$newDG = '192.168.2.1'


$esx = Get-VMHost -Name $esxName


$netMgr = Get-View -Id $esx.ExtensionData.ConfigManager.NetworkSystem

$config = New-Object VMware.Vim.HostNetworkConfig


$pg = $netMgr.NetworkConfig.Portgroup | where{$_.Spec.Name -eq $tgtPG}

$pg.ChangeOperation = [VMware.Vim.HostConfigChangeOperation]::edit

$pg.Spec.VlanId = $newVLAN

$config.Portgroup += $pg


$vmk = $netmgr.NetworkConfig.Vnic | where{$_.Device -eq $vmkName}

$vmk.ChangeOperation = [VMware.Vim.HostConfigChangeOperation]::edit

$vmk.Spec.IpRouteSpec = New-Object VMware.Vim.HostVirtualNicIpRouteSpec

$vmk.spec.IpRouteSpec.IpRouteConfig = New-Object VMware.Vim.HostIpRouteConfig

$vmk.Spec.IpRouteSpec.IpRouteConfig.DefaultGateway = $newDG

$config.Vnic += $vmk


$netMgr.UpdateNetworkConfig($config,[VMware.Vim.HostConfigChangeMode]::modify)


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

Reply
0 Kudos
TakoRoni
Enthusiast
Enthusiast
Jump to solution

thank you for the replay

why there is no value for portgroup ?

$esxName = 'esx1clal-ins'

$esx = Get-VMHost -Name $esxName

$netMgr = Get-View -Id $esx.ExtensionData.ConfigManager.NetworkSystem

$config = New-Object VMware.Vim.HostNetworkConfig

$netMgr.NetworkConfig.Portgroup

$pg  # no value

ChangeOperation Spec

--------------- ----

                VMware.Vim.HostPortGroupSpec

                VMware.Vim.HostPortGroupSpec

how can i see what is the right portgroup for this ESX

how do i change the ESX ip in this  script ?

thank you in advanse

Roni

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Are you connected to an ESXi node or a VCSA?


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

Reply
0 Kudos
TakoRoni
Enthusiast
Enthusiast
Jump to solution

hi

it was bad syntax from my side

the only thing that is missing is also change the ip address for the esx in the same script

i am connect with powercli to the Vcenter

roni

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Something like this?
Re: How To Change vmk0 IP


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

Reply
0 Kudos
TakoRoni
Enthusiast
Enthusiast
Jump to solution

wont it be a problem to run

Get-VMHost -Name MyEsx |

Get-VMHostNetworkAdapter -Name vmk1 |

Set-VMHostNetworkAdapter -IP '192.168.1.111'

inside the script - it will affect immediately  and not wait for the commit

$netMgr.UpdateNetworkConfig($config,[VMware.Vim.HostConfigChangeMode]::modify

can i do the same as the DG ?

$vmk = $netmgr.NetworkConfig.Vnic | where{$_.Device -eq $vmkName}

$vmk.ChangeOperation = [VMware.Vim.HostConfigChangeOperation]::edit

$vmk.Spec.IpRouteSpec = New-Object VMware.Vim.HostVirtualNicIpRouteSpec

$vmk.spec.IpRouteSpec.IpRouteConfig = New-Object VMware.Vim.HostIpRouteConfig

$vmk.Spec.IpRouteSpec.IpRouteConfig.DefaultGateway = $newDG

$config.Vnic += $vmk

i didnt find who to do it

roni

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The $vmk.Spec also has an IP property.

In there, with the HostIpConfig object, you can specify an IP address for the vmk.


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

Reply
0 Kudos
TakoRoni
Enthusiast
Enthusiast
Jump to solution

can you give example

i am confused with who to apply it in the script

roni

Reply
0 Kudos
TakoRoni
Enthusiast
Enthusiast
Jump to solution

is this OK

$vmk.spec.ip  = New-Object VMware.Vim.HostIpConfig

$Vmk.spec.ip.ipaddress = $newip

Reply
0 Kudos
TakoRoni
Enthusiast
Enthusiast
Jump to solution

this is the script

$esxName = 'ESXServer'

$tgtPg = 'Management Network'

$vmkName = 'vmk0'

$newVLAN = 111

$newDG = '1.1.1.1'

$newip = '1.1.1.5'

$esx = Get-VMHost -Name $esxName

$netMgr = Get-View -Id $esx.ExtensionData.ConfigManager.NetworkSystem

$config = New-Object VMware.Vim.HostNetworkConfig

$pg = $netMgr.NetworkConfig.Portgroup | where{$_.Spec.Name -eq $tgtPG}

$pg.ChangeOperation = [VMware.Vim.HostConfigChangeOperation]::edit

$pg.Spec.VlanId = $newVLAN

$config.Portgroup += $pg

$vmk = $netmgr.NetworkConfig.Vnic | where{$_.Device -eq $vmkName}

$vmk.ChangeOperation = [VMware.Vim.HostConfigChangeOperation]::edit

$vmk.Spec.IpRouteSpec = New-Object VMware.Vim.HostVirtualNicIpRouteSpec

$vmk.spec.IpRouteSpec.IpRouteConfig = New-Object VMware.Vim.HostIpRouteConfig

$vmk.Spec.IpRouteSpec.IpRouteConfig.DefaultGateway = $newDG

$vmk.spec.ip  = New-Object VMware.Vim.HostIpConfig

$Vmk.spec.ip.ipaddress = $newip

$config.Vnic += $vmk

$netMgr.UpdateNetworkConfig($config,[VMware.Vim.HostConfigChangeMode]::modify)

but i get this Error

Exception calling "UpdateNetworkConfig" with "2" argument(s): "A specified parameter was not correct: "

At C:\Scripts\VMware\change_esx_ip\changevlan.ps1:38 char:1

+ $netMgr.UpdateNetworkConfig($config,[VMware.Vim.HostConfigChangeMode] ...

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

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : VimException

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Yes, that should work for the IP address.

But I can't seem to find a method to change the VLAN.

When I try to change the Portgroup, I get an error.


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

Reply
0 Kudos
TakoRoni
Enthusiast
Enthusiast
Jump to solution

hi

it looks like the problem is in

$config.Portgroup += $pg

and then

$netMgr.UpdateNetworkConfig($config,[VMware.Vim.HostConfigChangeMode]::modify)

if we dont make the change and skip $config.Portgroup += $pg

there is no error

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Exactly, I stumbled on the same.
I wonder if the changes you intend, as possible via the DCUI, can be done this way.


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

Reply
0 Kudos
TakoRoni
Enthusiast
Enthusiast
Jump to solution

if you think of something i be glad

if anyone have idea of how to solve this

answer will be accepts with joey

roni.

Reply
0 Kudos
TakoRoni
Enthusiast
Enthusiast
Jump to solution

hi

found the problem

$newVLAN = 111

instead of

$newVLAN = '111'

2 problem

when i run

$pg = $netMgr.NetworkConfig.Portgroup | where{$_.Spec.Name -eq $tgtPG}

$pg.ChangeOperation = [VMware.Vim.HostConfigChangeOperation]::edit

$pg.Spec.VlanId = $newVLAN

$config.Portgroup += $pg

$netMgr.UpdateNetworkConfig($config,[VMware.Vim.HostConfigChangeMode]::modify)

i get

Exception calling "UpdateNetworkConfig" with "2" argument(s): "An error occurred while communicating with the remote

  1. host. Network configuration change disconnected the host 'esxhrzvdi220.clal-ins' from vCenter server and has been

rolled back."

it could be because i haven't  change yet the DG and IP

but when i try to run all together i get a lot of errors

Exception calling "UpdateNetworkConfig" with "2" argument(s): "

Unexpected element tag "ipRouteSpec" seen

while parsing serialized DataObject of type vim.host.VirtualNic.Specification

at line 1, column 383

while parsing property "spec" of static type HostVirtualNicSpec

while parsing serialized DataObject of type vim.host.VirtualNic.Config

at line 1, column 276

while parsing property "vnic" of static type ArrayOfHostVirtualNicConfig

while parsing serialized DataObject of type vim.host.NetworkConfig

at line 1, column 268

while parsing call information for method UpdateNetworkConfig

at line 1, column 171

while parsing SOAP body

at line 1, column 64

while parsing SOAP envelope

at line 1, column 0

while parsing HTTP request for method updateNetworkConfig

on object of type vim.host.NetworkSystem

at line 1, column 0"

At line:1 char:1

the script is as follow

************************************************

$esxName = 'EsxName'    #  replace it with yours Esxserver name

$tgtPg = 'Management Network'  # portGroup Name

$vmkName = 'vmk0'

$newVLAN = '11'   #  replace it with yours new vlan

$newDG = '1.1.1.1' #  replace it with yours new defult gateway

$newip = '1.1.1.5' #  replace it with yours new Esxi IP Address

$esx = Get-VMHost -Name $esxName

$netMgr = Get-View -Id $esx.ExtensionData.ConfigManager.NetworkSystem

$config = New-Object VMware.Vim.HostNetworkConfig

$pg = $netMgr.NetworkConfig.Portgroup | where{$_.Spec.Name -eq $tgtPG}

$pg.ChangeOperation = [VMware.Vim.HostConfigChangeOperation]::edit

$pg.Spec.VlanId = $newVLAN

$config.Portgroup += $pg

$vmk = $netmgr.NetworkConfig.Vnic | where{$_.Device -eq $vmkName}

$vmk.ChangeOperation = [VMware.Vim.HostConfigChangeOperation]::edit

$vmk.Spec.IpRouteSpec = New-Object VMware.Vim.HostVirtualNicIpRouteSpec

$vmk.spec.IpRouteSpec.IpRouteConfig = New-Object VMware.Vim.HostIpRouteConfig

$vmk.Spec.IpRouteSpec.IpRouteConfig.DefaultGateway = $newDG

$vmk.spec.ip  = New-Object VMware.Vim.HostIpConfig

$Vmk.spec.ip.ipaddress = $newip

$config.Vnic += $vmk

$netMgr.UpdateNetworkConfig($config,[VMware.Vim.HostConfigChangeMode]::modify)

***********************************************************************

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Have a look in the vpxd.log on the VCSA, which sometimes provides more info on a failed call to a method.

You also might try setting the VCSA logging level from Info to Verbose.


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

Reply
0 Kudos
TakoRoni
Enthusiast
Enthusiast
Jump to solution

here is the script

now it works Smiley Happy

only thing that remain is to add a CSV file and run it with it

******************************************************************

$esxName = 'esx01'

$tgtPg = 'Management Network'  #portGroup

$vmkName = 'vmk0'

$newVLAN = '111'

$newDG = '1.1.1.1'

$newip = '1.1.1.10'

$stackName = 'defaultTcpipStack'

$esx = Get-VMHost -Name $esxName

$netMgr = Get-View -Id $esx.ExtensionData.ConfigManager.NetworkSystem

$config = New-Object VMware.Vim.HostNetworkConfig

#change Vlan ID

$pg = $netMgr.NetworkConfig.Portgroup | where{$_.Spec.Name -eq $tgtPG}

$pg.ChangeOperation = [VMware.Vim.HostConfigChangeOperation]::edit

$pg.Spec.VlanId = $newVLAN

$config.Portgroup += $pg

#change Defult Gateway

#$netMgr = Get-View -Id $esx.ExtensionData.ConfigManager.NetworkSystem

$stack = $esx.ExtensionData.Config.Network.NetStackInstance | where{$_.Key -eq $stackName}

$spec = New-Object VMware.Vim.HostNetworkConfigNetStackSpec

$spec.Operation = [VMware.Vim.ConfigSpecOperation]::edit

$spec.NetStackInstance = $stack

$spec.NetStackInstance.RouteTableConfig = New-Object VMware.Vim.HostIpRouteTableConfig

$route = New-Object VMware.Vim.HostIpRouteConfig

$route.defaultGateway = $newDG

$route.gatewayDevice = $vmkName

$stack.IpRouteConfig.DefaultGateway = $newDG

$spec.NetStackInstance.ipRouteConfig = $route

#change ip address to ESXi

$vmk = $netmgr.NetworkConfig.Vnic | where{$_.Device -eq $vmkName}

$vmk.ChangeOperation =[VMware.Vim.ConfigSpecOperation]::edit

$vmk.Spec.Ip.IpAddress= $newip

# close and commit changes

$config.NetStackSpec += $spec

$config.Vnic += $vmk

$netMgr.UpdateNetworkConfig($config,[VMware.Vim.HostConfigChangeMode]::modify)

*********************************************************

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I assume that will not be an issue?


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

Reply
0 Kudos