VMware Cloud Community
vin01
Expert
Expert
Jump to solution

Migrate Network from Standard switch to VDS

Hi

I'm planning to migrate few of my clusters which are running in standard switch to DVS.Need help to automate this process.

My scenario is :

In a environment 15cluster are present 5 are already running in DVS
but 10 clusters are still in standard switch so I need to migrate them
to already existing DVSwitch.

Taking one 10node cluster as example.
1)mgmt and vmkernel are on one vswitch (ex: vswitch 0)
[vswitch 0 --- Connected with 2 vmincs ]

2)virtual machine portgroup in one vswitch (Ex: vswitch 1)
[vswitch 1 --- Connected with 2 vmincs ]

On all the clusters we need to migrate only virtual machine portgroup
or vmnetwork (ex:vswitch 1) to DVs excluding mgmt and vmkernel network.
My plan:
First add all the hosts in cluster to DVswitch
Remove any one redundant vmnic (ex:vmnic3) connected to virtual machine portgroup. (Ex: vswitch 1) and add this to DVswitch.
Now migrate all vms connected to standard switch to dvs.(Here i struck)
Is there any way if VLAN 10 (standard switch) consists of 5 VMS then
check VLAN 10 in DVS and set network adapter portgroup to VLAN 10 on the
vms (I mean can we compare VLAN IDs on VSS and VDS and based on than we
need to migrate vms to DVS.)

As below :

VLAN_58_VDS.JPG

VLAN_58_VSSD.JPG
If it works successfully then need to check ping status of all vms (ignore powered off vms)
If ok then proceed to map second nic (ex:vmnic4) from VSS to VDS.

Used Script:

# ESXi hosts to migrate from VSS to VDS

$vmhost_array = @("host1","host2")

# Adding Hosts to existing VDswitch

$vds = Get-VDSwitch -Name "SM_UAT"

foreach ($vmhost in $vmhost_array) {

# Add ESXi host to VDS

Write-Host "Adding" $vmhost "to" $vds_name

$vds | Add-VDSwitchVMHost -VMHost $vmhost | Out-Null

# Migrate pNICs to VDS

Write-Host "Adding vmnic2 to" $vds

$vmhostNetworkAdapter = Get-VMHost $vmhost |Get-VirtualSwitch -Name "vswitch1" |select -ExpandProperty Nic |select -First 1

$vds | Add-VDSwitchPhysicalNetworkAdapter -VMHostNetworkAdapter $vmhostNetworkAdapter -Confirm:$false

# Migrate VMs from VSS to VDS

Write-Host "migrating vms from VSS to VDS"

}


Regards Vineeth.K
57 Replies
vin01
Expert
Expert
Jump to solution

Yes I am trying to migrate such type of VLANs before executing main script.One last question in our master script we tried to fetch vswitch1 details as below

Write-Host  -Object 'Fetching virtualPortgroup info from vswitch'

$vssPG1 = $vmhost |Get-VirtualPortGroup  -Standard -VirtualSwitch 'vswitch1'

So is it possible to get any vswitch details where "virtual machine port group" exists excluding vmkernel & management Network.

For ex: Vswitch 1 consists of "virtual machine port group" network and  Vswitch 2 consists of "virtual machine port group" network

But Vswitch 0 consists of vmkernel & management Network. I need to exclude vswitch 0.

Regards Vineeth.K
0 Kudos
LucD
Leadership
Leadership
Jump to solution

0 Kudos
vin01
Expert
Expert
Jump to solution

On that post already Vswitch 0 was given and there it is filtered but I need filter at switch level.

For ex: Vswitch 0 contains vmkernel & mgmt network

Vswitch 1 and Vswitch2 contains virtual machine port group Here i need to filter or exclude Vswitch0 is it possible.

vswitcherror.JPG

Regards Vineeth.K
0 Kudos
LucD
Leadership
Leadership
Jump to solution

What should be done when vSwitch0 also contains a VM network portgroup ?

If you only want the switches that do not have vmkernel portgroups, you can do

Get-VirtualSwitch -VMHost $vmhost |

where {Get-VirtualPortGroup -VirtualSwitch $_ | where {@('host') -notcontains $_.port}}


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

vin01
Expert
Expert
Jump to solution

In this environment where i am going to execute the migration script doesn't contains VM network portgroup on Vswitch0 on any cluster or standalone host for that reason i am planning to pull multiple switches at one instance.

The below given filter work perfectly.Thanks once again Master Smiley Happy Ill try it by adding this filter and post you back as of now i am marking the above code as correct.

Regards Vineeth.K
0 Kudos
vin01
Expert
Expert
Jump to solution

Luc, If statement is not working when I tried to execute on some clusters all vms are successfully migrated to DVS but still Second nic connected to Vswitch 1 is not removed and attached to DVS.

Actually i am looking as below..

If all vms connected to Vswitch 1 are migrated to DVS then it should remove second Nic connected to Vswitch 1 and that vmnic should connects to DVS.

If vms exists on Vswitch 1 then it should show  Still VMs connected to vswitch1 on $($vmhost.Name).

Regards Vineeth.K
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Are there perhaps other portgroups connected to the virtual switch ?

What does

Get-VirtualSwitch -Name 'vswitch1' -VMHost $vmhost | Get-VirtualPortGroup

return when all VMs have been migrated ?


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

0 Kudos
vin01
Expert
Expert
Jump to solution

Oops i deleted the vswitch 1 manually after completion of migration.I will give a try on other hosts and post you back.

Regards Vineeth.K
0 Kudos
vin01
Expert
Expert
Jump to solution

Hi Luc

This is the Output

Get-VirtualSwitch -Name 'vswitch1' -VMHost $vmhost | Get-VirtualPortGroup


vswitch1_.JPG

vswitch1.JPG

Regards Vineeth.K
0 Kudos
LucD
Leadership
Leadership
Jump to solution

So when you try to remove the VSS it fails, or not ?

Perhaps you should first try to remove the virtual portgroups.

Does that make a difference ?


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

0 Kudos
vin01
Expert
Expert
Jump to solution

So when you try to remove the VSS it fails, or not ?

No I didn't added any lines to remove VSS or portgroups in the main script which i marked as correct.

===

The below give lines are added to main script which i marked as correct to remove second vnic when all vms migrated from VSS to VDS.

If VMS exists on VSS then its should show 'Still VMs connected to vswitch1 on $($vmhost.Name)'.

When i executed on a host all VMS are migrated from VSS to VDS but its not removing the second nic connected to VSwitch 1 as per below code it should remove the redundant nic connected to vswitch1 when there is no VM on Vswitch 1.

Used Code :

Write-Host  -Object 'Fetching Second vnic connected to vswitch'

  if(!(Get-VirtualSwitch -Name 'vswitch1' -VMHost $vmhost | Get-VM)){

     $vmhostNetworkAdapter2 = Get-VMHost $vmhost |

     Get-VirtualSwitch -Name 'vswitch1' | Select-Object -ExpandProperty Nic | Select-Object -First 1 

     Write-Host  -Object 'Removing Second vnic from vswitch' 

     $vmhost |Get-VMHostNetworkAdapter -Name $vmhostNetworkAdapter2 |

     Remove-VirtualSwitchPhysicalNetworkAdapter -Confirm:$false

     Write-Host  -Object 'Fetching Second removed vnic into a varible' 

     $Phnic2 = $vmhost |Get-VMHostNetworkAdapter -Physical -Name $vmhostNetworkAdapter2 

     Write-Host  -Object "Adding Second vmnic (redundant) to $vds" 

     Add-VDSwitchPhysicalNetworkAdapter -DistributedSwitch $vds -VMHostPhysicalNic $Phnic2 -Confirm:$false

   }

   else

   {

     Write-Host 'Still VMs connected to vswitch1 on $($vmhost.Name)'

   }

==============

Complete Code  :


# ESXi hosts to migrate from VSS to VDS

$vmhost_array = Get-Cluster "SUN-UAT" |Get-VMHost

# get existing VDswitch

$vds = Get-VDSwitch -Name 'SM_UAT'

foreach ($vmhost in $vmhost_array)

{

# Add ESXi hosts to VDS

Write-Host  -Object "Adding $vmhost to $vds"

$null = $vds |Add-VDSwitchVMHost -VMHost $vmhost

# Migrate pNICs to VDS

Write-Host  -Object 'Fetching First vnic connected to vswitch'

$vmhostNetworkAdapter = Get-VMHost $vmhost |Get-VirtualSwitch -Name 'vswitch1' | Select-Object -ExpandProperty Nic | Select-Object -First 1

Write-Host  -Object 'Removing vnic from vswitch'

$vmhost |Get-VMHostNetworkAdapter -Name $vmhostNetworkAdapter |Remove-VirtualSwitchPhysicalNetworkAdapter -Confirm:$false

Write-Host  -Object 'Fetching removed vnic into a varible'

$Phnic = $vmhost |Get-VMHostNetworkAdapter -Physical -Name $vmhostNetworkAdapter

Write-Host  -Object "Adding vmnic (redundant) to $vds"

Add-VDSwitchPhysicalNetworkAdapter -DistributedSwitch $vds -VMHostPhysicalNic $Phnic -Confirm:$false

# Migrate VMs from VSS to VDS

Write-Host  -Object 'Fetching virtualPortgroup info from vswitch'

$vssPG1 = $vmhost |Get-VirtualPortGroup  -Standard -VirtualSwitch 'vswitch1'

Write-Host  -Object 'migrating vms from VSS to VDS'

foreach($vssPG in $vssPG1)

{

$dvsPG = $vds |Get-VDPortgroup |Where-Object  -FilterScript {$_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId -eq $vssPG.VlanId}

$vmhost |Get-VM |Get-NetworkAdapter |Where-Object  -FilterScript {$_.NetworkName -eq $vssPG.Name} | Set-NetworkAdapter -NetworkName $dvsPG.Name -Confirm:$false

}

# Check Ping Status

#Write-Host -Object "Check Ping Status of VMS and write output"

#$vmhost |Get-VM | Select Name,PowerState,@{N="Ping";E={

#if($_.PowerState -eq "PoweredOn" -and (Test-Connection -ComputerName $_.Name)){"Online"}else{"Offline"}}}

Write-Host  -Object 'Fetching Second vnic connected to vswitch'

$vmhostNetworkAdapter2 = Get-VMHost $vmhost |Get-VirtualSwitch -Name 'vswitch1' | Select-Object -ExpandProperty Nic | Select-Object -First 1

Write-Host  -Object 'Fetching Second vnic connected to vswitch'

if(!(Get-VirtualSwitch -Name 'vswitch1' -VMHost $vmhost | Get-VM)){

$vmhostNetworkAdapter2 = Get-VMHost $vmhost |Get-VirtualSwitch -Name 'vswitch1' | Select-Object -ExpandProperty Nic | Select-Object -First 1 

Write-Host  -Object 'Removing Second vnic from vswitch' 

$vmhost |Get-VMHostNetworkAdapter -Name $vmhostNetworkAdapter2 |Remove-VirtualSwitchPhysicalNetworkAdapter -Confirm:$false

Write-Host  -Object 'Fetching Second removed vnic into a varible' 

$Phnic2 = $vmhost |Get-VMHostNetworkAdapter -Physical -Name $vmhostNetworkAdapter2 

Write-Host  -Object "Adding Second vmnic (redundant) to $vds" 

Add-VDSwitchPhysicalNetworkAdapter -DistributedSwitch $vds -VMHostPhysicalNic $Phnic2 -Confirm:$false

   }

   else

   {

     Write-Host 'Still VMs connected to vswitch1 on $($vmhost.Name)'

   }

   }

Regards Vineeth.K
0 Kudos
vin01
Expert
Expert
Jump to solution

Hi Luc

waiting for comments on the above post.Should i change anything in the code.

Regards Vineeth.K
0 Kudos
LucD
Leadership
Leadership
Jump to solution

No, that code looks ok.

And if it does what you want it to do, it is ok.


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

0 Kudos
vin01
Expert
Expert
Jump to solution

But its failing from below lines..even though all vms migrated from vswitch1 to vds it should remove second vnic and it should connect that vnic to vds but its showing 'Still VMs connected to vswitch1 on $($vmhost.Name)' .what could be the issue?

Write-Host  -Object 'Fetching Second vnic connected to vswitch' 
  if(!(Get-VirtualSwitch -Name 'vswitch1' -VMHost $vmhost | Get-VM)){ 
     $vmhostNetworkAdapter2 = Get-VMHost $vmhost | 
     Get-VirtualSwitch -Name 'vswitch1' | Select-Object -ExpandProperty Nic | Select-Object -First 1   
     Write-Host  -Object 'Removing Second vnic from vswitch'   
     $vmhost |Get-VMHostNetworkAdapter -Name $vmhostNetworkAdapter2 | 
     Remove-VirtualSwitchPhysicalNetworkAdapter -Confirm:$false  
     Write-Host  -Object 'Fetching Second removed vnic into a varible'   
     $Phnic2 = $vmhost |Get-VMHostNetworkAdapter -Physical -Name $vmhostNetworkAdapter2   
     Write-Host  -Object "Adding Second vmnic (redundant) to $vds"   
     Add-VDSwitchPhysicalNetworkAdapter -DistributedSwitch $vds -VMHostPhysicalNic $Phnic2 -Confirm:$false 
  
   else 
  
     Write-Host 'Still VMs connected to vswitch1 on $($vmhost.Name)' 
  

Regards Vineeth.K
0 Kudos
LucD
Leadership
Leadership
Jump to solution

And is

Get-VirtualSwitch -Name 'vswitch1' -VMHost $vmhost | Get-VM

returning anything or not at that moment ?


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

0 Kudos
vin01
Expert
Expert
Jump to solution

yes there is the problem..When I try to fetch

Get-VirtualSwitch -Name 'vswitch1' -VMHost $vmhost | Get-VM

Its listing all the vms which are connected to VSS on all the hosts in Datacenter not the host in  $vmhost variable.

Regards Vineeth.K
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Could it be that the content of the $vmhost variable at that time is incorrect ?

Does it also return the VMs for all ESXi nodes, when you give the hostname as a string on the parameter

Get-VirtualSwitch -Name 'vswitch1' -VMHost 'myEsx' | Get-VM


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

0 Kudos
vin01
Expert
Expert
Jump to solution

yes i tried that too..But same result its writing vms of all esxi nodes.

as below

Get-VirtualSwitch -Name 'vswitch1' -VMHost 10.xx.xx.xx | Get-VM

Regards Vineeth.K
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I can confirm (in PowerCLI 5.8R1), there seems to be indeed a "feature" in the Get-VM cmdlet with the VirtualSwitch parameter.

As a bypass, you can use something like this

(Get-View (Get-View (Get-VirtualSwitch -Name vSwitch1 -VMHost $vmhost ).VMHostId).VM | where {!$_.Config.Template}).Count


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

0 Kudos
vin01
Expert
Expert
Jump to solution

Thats PerfectSmiley Happy.Its giving no.of vms present in vswitch1.Can you rewrite the below code to remove the second nic in vswitch 1 when there is no vm count.


(Get-View (Get-View (Get-VirtualSwitch -Name vSwitch1 -VMHost $vmhost ).VMHostId).VM | where {!$_.Config.Template}).Count


Write-Host  -Object 'Fetching Second vnic connected to vswitch'

if(!(Get-VirtualSwitch -Name 'vswitch1' -VMHost $vmhost | Get-VM)){

$vmhostNetworkAdapter2 = Get-VMHost $vmhost |Get-VirtualSwitch -Name 'vswitch1' | Select-Object -ExpandProperty Nic | Select-Object -First 1 

Write-Host  -Object 'Removing Second vnic from vswitch' 

$vmhost |Get-VMHostNetworkAdapter -Name $vmhostNetworkAdapter2 |Remove-VirtualSwitchPhysicalNetworkAdapter -Confirm:$false

Write-Host  -Object 'Fetching Second removed vnic into a varible' 

$Phnic2 = $vmhost |Get-VMHostNetworkAdapter -Physical -Name $vmhostNetworkAdapter2 

Write-Host  -Object "Adding Second vmnic (redundant) to $vds" 

Add-VDSwitchPhysicalNetworkAdapter -DistributedSwitch $vds -VMHostPhysicalNic $Phnic2 -Confirm:$false

   }

   else

   {

     Write-Host 'Still VMs connected to vswitch1 on $($vmhost.Name)'

   }

   }

Regards Vineeth.K
0 Kudos