Automation

 View Only
Expand all | Collapse all

script to change vlanid ip and gateway for ESXi

  • 1.  script to change vlanid ip and gateway for ESXi

    Posted Jul 14, 2020 11:07 AM

    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



  • 2.  RE: script to change vlanid ip and gateway for ESXi

    Posted Jul 14, 2020 12:25 PM

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



  • 3.  RE: script to change vlanid ip and gateway for ESXi

    Posted Jul 14, 2020 04:35 PM

    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)



  • 4.  RE: script to change vlanid ip and gateway for ESXi

    Posted Jul 27, 2020 05:02 AM

    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



  • 5.  RE: script to change vlanid ip and gateway for ESXi

    Posted Jul 27, 2020 06:15 AM

    Are you connected to an ESXi node or a VCSA?



  • 6.  RE: script to change vlanid ip and gateway for ESXi

    Posted Jul 27, 2020 07:25 AM

    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



  • 7.  RE: script to change vlanid ip and gateway for ESXi

    Posted Jul 27, 2020 07:32 AM

    Something like this?
    Re: How To Change vmk0 IP



  • 8.  RE: script to change vlanid ip and gateway for ESXi

    Posted Jul 27, 2020 08:04 AM

    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



  • 9.  RE: script to change vlanid ip and gateway for ESXi

    Posted Jul 27, 2020 08:53 AM

    The $vmk.Spec also has an IP property.

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



  • 10.  RE: script to change vlanid ip and gateway for ESXi

    Posted Jul 27, 2020 09:03 AM

    can you give example

    i am confused with who to apply it in the script

    roni



  • 11.  RE: script to change vlanid ip and gateway for ESXi

    Posted Jul 27, 2020 11:02 AM

    is this OK

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

    $Vmk.spec.ip.ipaddress = $newip



  • 12.  RE: script to change vlanid ip and gateway for ESXi

    Posted Jul 27, 2020 11:11 AM

    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



  • 13.  RE: script to change vlanid ip and gateway for ESXi

    Posted Jul 27, 2020 11:12 AM

    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.



  • 14.  RE: script to change vlanid ip and gateway for ESXi

    Posted Jul 27, 2020 02:45 PM

    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



  • 15.  RE: script to change vlanid ip and gateway for ESXi

    Posted Jul 27, 2020 02:49 PM

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



  • 16.  RE: script to change vlanid ip and gateway for ESXi

    Posted Jul 27, 2020 03:51 PM

    if you think of something i be glad

    if anyone have idea of how to solve this

    answer will be accepts with joey

    roni.



  • 17.  RE: script to change vlanid ip and gateway for ESXi

    Posted Jul 27, 2020 05:47 PM

    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)

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



  • 18.  RE: script to change vlanid ip and gateway for ESXi

    Posted Jul 27, 2020 05:52 PM

    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.



  • 19.  RE: script to change vlanid ip and gateway for ESXi
    Best Answer

    Posted Jul 28, 2020 12:22 PM

    here is the script

    now it works :smileyhappy:

    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)

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



  • 20.  RE: script to change vlanid ip and gateway for ESXi

    Posted Jul 28, 2020 12:30 PM

    I assume that will not be an issue?



  • 21.  RE: script to change vlanid ip and gateway for ESXi