Virtual Kim

@Kim_Bottu … Virtually Gung Ho

Create dVS and migrate vSwitch

This script is based on the script William Lam created in 2013. You can find it at http://www.virtuallyghetto.com/2013/10/automate-migration-from-virtual.html.
In a scenario where you only have 2 physical NICs per host, where both NICs are combined in a portchannel and loadbalancing has been set to IPHASH, walking over NIC by NIC, worked better for me.

# Create Distributed Virtual Switch “YOURdvSwtch” for the hosts mentioned in “vmhost_array”.
# Create 3 different distributed portgroups (Management Network, 192.168.10.0 and 192.168.11.0)
# Starting from vSphere 5.5 adding the number of ports per portgroup is not needed anymore, ports are assigned elastically.
# If you use vSphere 5.5 and up, you can remoe the entries for -NumPorts
# Set the VLANID and set the Uplink Teaming policy to LoadbalanceIP
# Migrate vmnic0
# Migrate the Management Network
# Migrate vmnic1
# Migrate vMotion
# Delete the vSwitch0

#In the following lines replace the ESXi servers with a list of your ESXi servers.

Write-Host #ESXi hosts to migrate from vSwitch to dVS

$vmhost_array = @(“ESX01.host.local”, “ESX02.host.local”, “ESX03.host.local”, “ESX04.host.local”)

#In the following lines replace “YOURdvSwitch” with the name you want to give to your new DVS.

Write-Host #Create VDS

$vds_name = “YOURdvSwitch”
Write-Host “`Creating new VDS” $vds_name
$vds = New-VDSwitch -Name $vds_name -Location (Get-Datacenter -Name “TEST”)

#In the following lines #Create DVPortgroups

Write-Host “Creating new Management Network”

New-VDPortgroup -Name “Management Network” -Vds $vds | Out-Null

Write-Host “Creating new 192.168.1.0 DVPortgroup”

New-VDPortgroup -Name “192.168.1.0” -Vds $vds | Out-Null

Write-Host “Creating new 192.168.2.0 DVPortgroup”

New-VDPortgroup -Name “192.168.2.0” -Vds $vds | Out-Null

Write-Host “Creating new vMotion DVPortgroup”

New-VDPortgroup -Name “vMotion” -Vds $vds | Out-Null

Add VLANs, Portgroups and NIC TEAMING POLICY to DVPortgroups

Write-Host “Add VLANs, Portgroups and NIC TEAMING POLICY to DVPortgroups”

Get-VDSwitch “YOURdvSwitch” | Get-VDPortgroup -Name “Management Network” | Set-VDPortgroup -Name “Management Network” -NumPorts 128 | Get-VDUplinkTeamingPolicy | Set-VDUplinkTeamingPolicy -LoadBalancingPolicy LoadBalanceIP
Get-VDSwitch “YOURdvSwitch” | Get-VDPortgroup -Name “192.168.1.0” | Set-VDPortgroup -Name “192.168.1.0” -NumPorts 128 -VlanId 1 | Get-VDUplinkTeamingPolicy | Set-VDUplinkTeamingPolicy -LoadBalancingPolicy LoadBalanceIP
Get-VDSwitch “YOURdvSwitch” | Get-VDPortgroup -Name “192.168.2.0” | Set-VDPortgroup -Name “192.168.2.0” -NumPorts 128 -VlanId 2 | Get-VDUplinkTeamingPolicy | Set-VDUplinkTeamingPolicy -LoadBalancingPolicy LoadBalanceIP
Get-VDSwitch “YOURdvSwitch” | Get-VDPortgroup -Name “vMotion” | Set-VDPortgroup -Name “vMotion” -NumPorts 128 -VlanId 50 | Get-VDUplinkTeamingPolicy | Set-VDUplinkTeamingPolicy -LoadBalancingPolicy LoadBalanceIP
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 pNIC to VDS (vmnic0) – vSWITCH StandBy NIC

Write-Host “vmnic0 to” $vds_name

$vmhostNetworkAdapter = Get-VMHost $vmhost | Get-VMHostNetworkAdapter -Physical -Name vmnic0

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

# Migrate VMkernel interfaces to VDS

# Management

$mgmt_portgroup = “Management Network”
Write-Host “Migrating” $mgmt_portgroup “to” $vds_name
$dvportgroup = Get-VDPortgroup -name $mgmt_portgroup -VDSwitch $vds
$vmk = Get-VMHostNetworkAdapter -Name vmk0 -VMHost $vmhost
Set-VMHostNetworkAdapter -PortGroup $dvportgroup -VirtualNic $vmk -confirm:$false | Out-Null

# Migrate pNIC to VDS (vmnic1) – vSWITCH Active NIC

Write-Host “vmnic1 to” $vds_name

$vmhostNetworkAdapter = Get-VMHost $vmhost | Get-VMHostNetworkAdapter -Physical -Name vmnic1

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

# vMotion #

$vMotion = “vMotion”

Write-Host “Migrating” $vMotion “to” $vds_name

$dvportgroup = Get-VDPortgroup -name $vMotion -VDSwitch $vds
$vmk = Get-VMHostNetworkAdapter -Name vmk1 -VMHost $vmhost
Set-VMHostNetworkAdapter -PortGroup $dvportgroup -VirtualNic $vmk -confirm:$false | Out-Null
}

# Next script will remove the existing vSwitch on all hosts

Write-Host Delete vSwitch0

$vmhost_array = @(“ESX01.host.local”, “ESX02.host.local”, “ESX03.host.local”, “ESX04.host.local”)

$vSwitch= “vSwitch0”

foreach ($vmhost in $vmhost_array) {
Remove-VirtualSwitch -VirtualSwitch $vSwitch -confirm:$false | Out-Null
}

Write-Host

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Information

This entry was posted on February 18, 2015 by in Distributed Switch, dVS, PowerCLI, Script and tagged , , , .
Follow Virtual Kim on WordPress.com

Blog Stats

  • 26,240 hits
%d bloggers like this: