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.