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 :
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"
}
Perhaps try like this
# ESXi hosts to migrate from VSS to VDS
$vmhost_array = Get-VMHost -Name 10.50.37.31
# get existing VDswitch
$vds = Get-VDSwitch -Name 'Pre-Production'
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 = $vmst |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 -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
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Did you already look at William's excellent post Automate the migration from Virtual Standard Switch to vSphere Distributed Switch using PowerCLI 5.5
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes Luc.I gone through that post..Based on that i written a sample code but where i missed on this step..After adding one redudant vmnic from VSS to VDS.I need to migrate VMS by comparing VLANS because only VLAN will be common on both VSS and VDS.
For example if VLAN 1 consists of 3vms in VSS they should migrate to VLAN 1 on DVS( VLAN Name might be differ from VSS and VDS but VLAN ID will be common so based on this is it possible Set-NetworkAdapter -Portgroup for vms.)
Try something like this.
In short:
foreach($vssPG in Get-VirtualPortGroup -VirtualSwitch 'vswitch1'){
$dvsPG = Get-VirtualPortGroup -Distributed |
where {$_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId -eq $vssPG.VlanId}
Get-VM | Get-NetworkAdapter | where {$_.NetworkName -eq $vssPG.Name}} |
Set-NetworkAdapter -NetworkName $dvsPG.Name
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Luc..Thanks for the code..I written the code as below and executed on single esxi host it works to some extend but its not looping on all VLANS.Only one VLAN vms are migrating to DVS.Can you correct me..
# ESXi hosts to migrate from VSS to VDS
$vmhost_array = Get-VMHost 10.50.37.31
# get existing VDswitch
$vds = Get-VDSwitch -Name "Pre-Production"
foreach ($vmhost in $vmhost_array) {
# Add ESXi hosts to VDS
Write-Host "Adding" $vmhost "to" $vds
$vds | Add-VDSwitchVMHost -VMHost $vmhost | Out-Null
# Migrate pNICs to VDS
Write-Host "Fetching First vnic connected to vswitch"
$vmhostNetworkAdapter = Get-VMHost $vmhost |Get-VirtualSwitch -Name "vswitch1" |select -ExpandProperty Nic |select -First 1
Write-Host "Removing vnic from vswitch"
$vmhost |Get-VMHostNetworkAdapter -Name $vmhostNetworkAdapter | Remove-VirtualSwitchPhysicalNetworkAdapter -Confirm:$false
Write-Host "Fetching removed vnic into a varible"
$Phnic= $vmst |Get-VMHostNetworkAdapter -Physical -Name $vmhostNetworkAdapter
Write-Host "Adding vmnic (redundant) to" $vds
Add-VDSwitchPhysicalNetworkAdapter -DistributedSwitch $vds -VMHostPhysicalNic $Phnic -Confirm:$false
# Migrate VMs from VSS to VDS
Write-Host "Fetching virtualPortgroup info from vswitch"
$vssPG1 =$vmhost|Get-VirtualPortGroup -VirtualSwitch 'vswitch1'
Write-Host "migrating vms from VSS to VDS"
foreach($vssPG in $vssPG1){
$dvsPG = $vds | Get-VDPortgroup | where {$_.ExtensionData.Config.DefaultPortConfig.Vlan.VlanId -eq $vssPG.VlanId}
$vmsnetname =$vmhost |Get-VM | Get-NetworkAdapter | where {$_.NetworkName -eq $vssPG.Name}}
$vmsnetname | Set-NetworkAdapter -NetworkName $dvsPG.Name
}
Perhaps try like this
# ESXi hosts to migrate from VSS to VDS
$vmhost_array = Get-VMHost -Name 10.50.37.31
# get existing VDswitch
$vds = Get-VDSwitch -Name 'Pre-Production'
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 = $vmst |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 -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
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Luc. Now its working all VMs are successfully migrated from standard Switch to VDS but its showing errors in output does it loops all VLANs because some vlans in VSS consists of no vms could it cause below errors.
Set-NetworkAdapter : Cannot validate argument on parameter 'NetworkName'. The argument is null. Provide a valid value for the argument, and
then try running the command again.
At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\standardtodvswitchmigration.ps1:26 char:133
+ ... r -NetworkName $dvsPG.Name -Confirm:$false
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Set-NetworkAdapter], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.SetNetworkAdapter
Set-NetworkAdapter : Cannot validate argument on parameter 'NetworkName'. The argument is null. Provide a valid value for the argument, and
then try running the command again.
At C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\standardtodvswitchmigration.ps1:26 char:133
+ ... r -NetworkName $dvsPG.Name -Confirm:$false
+ ~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Set-NetworkAdapter], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.VirtualDevice.SetNetworkAdapter
=================
Used Script :
# ESXi hosts to migrate from VSS to VDS
$vmhost_array = Get-VMHost 10.50.37.31
# get existing VDswitch
$vds = Get-VDSwitch -Name 'Pre-Production'
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 -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
}
}
$dvsPG seems to be empty.
It looks as if the no portgroup with the same VlanId was found on the VDS.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I think thats not issue..Because currently there are 10 Vlans in VSS which are exactly created on VDS but in that 10 VLANS only in 4 Vlans vms are running leftover vlans are empty in VSS is that could be causing that errors.If so is it possible to ignore those errors by add variable -erroraction 'silentlycontinue'.
Yes, you can.
But then probably some vNICs will not be migrated.
I'm curious to hear if you succeed in migrating all the vNICs.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
yes with the the above script I can able to migrate all the Vms from VSS to VDS Successfully but I'm looking why those error showed in output.In my assumption if a VLAN doesn't consist of any VM to change its throwing error.
for ex: VLAN 10 has no Vms in VSS it tries to check and it fails by throwing the error..Is it correct Can you try like this..
Creating 3 VLANS and keeping VMS only in 2 VLANS and run the above code.
The script tries to find a portgroup on a VDS that has the same VlanId as the VSS portgroup to which is connected.
I still can't see how you get the error message where it apparently doesn't find a VDS portgroup with the same VlanId.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
ok I'll try to execute on few other hosts in the cluster I'll post you back.
Tried on other hosts still showing same error:smileyconfused: But all vms adapters are Set from VSS to VDS successfully.From the below script is it possible check if any vms are present in "VSwitch 1" while removing second NIC.
For Ex: "Adding Second vmnic (redundant) to $vds" -- While removing second vnic from vswitch if any vms still present in VSS due to non availability of VLAN on VDS it will cause network outage So the script should stop if any vm still present on Vswitch 1.
Used Script :
# ESXi hosts to migrate from VSS to VDS
$vmhost_array = Get-VMHost 10.50.37.46
# get existing VDswitch
$vds = Get-VDSwitch -Name 'Pre-Production'
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 '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
}
Replace lines 32-39 by this
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)'
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
thanks Luc I'll give a try..One question can I goahead to execute on whole cluster by changing
Yes, that is how you would do it for all ESXi nodes in the cluster
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Ok I'll try it and post back the result.
Thanks Luc, Now its working perfectly but there is a problem in my environment with same VLAN number there are 2 or 3 VLANS .For Ex :
10.50.65.1-254_V71 | VLAN | 71 |
SM_10.24.155.1-254_V71 | VLAN | 71 |
Here its getting failed is there any other clues by changing the above code can we perform to change network name to this type of VLANS .If not this should be the better way i think.
Get-Cluster "SM" |Get-VMHost |Get-VM |Get-NetworkAdapter |where {$_.NetworkName -eq "10.24.155.1-254_V71"} |Set-NetworkAdapter -NetworkName "SM_10.24.155.1-254_V71" -Confirm:$false.
That is indeed a problem, the script doesn't handle multiple portgroups with the same VlanId.
In your solution you are migrating all VMs that sit on such a portgroup with the same VlanId, to another portgroup.
That should do the trick, if you execute this before the migration script
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference