Migrate Host and Guests VDS to SS using Powercli 5.5

Here is the follow up to my previous posting Migrate Host and Guests from SS to VDS using Powercli 5.5

Requirements for script: Host with 2 physical interfaces, VSwitch must already be setup with VM portgroups with the same names as you have on the VDS. If you have more than 2 physical nics you can modify the script below with the remove and add adapter commands for all your NICS. There are plenty of scripts out there to copy the VDS to Vswitch port groups if that task needs to be done.

The script below will allow you to migrate your host and guests back to Standard Switch! There was one issue I found with the Add-VirtualSwitchPhysicalNetworkAdapter cmdlet when migrating VMkernel ports. Instead of being able to name the portgroup as it migrates, it takes the default name of VMkernel and VMkernel2 for Management and Vmotion respectively. Be sure to also hardcode the Vlan ID in the Set-VirtualPortGroup rename cmdlet below for your tags.

So, after migrating them we run a separate command to have those Vmkernel ports renamed. You can modify the script to name them how you would like. I named them Management Network and VMotion.

The script will prompt you for the host you want to migrate, the VDswitch name you want to migrate off of, and the Vswitch you want to migrate to.

It will move 1 interface over with the 2 Vmkernel ports VMotion/Management, loop through all interfaces of the VM’s and set the portgroups to the matching standard switch groups, migrate 2nd interface, and remove from VDS.

$VMHost = Read-Host "Enter Hostname to Migrate"
$VDSwitch = Read-Host "Enter VDSwitch Name"
$VSwitch = Read-Host "Enter Standard VSwitch Name"

#Get VMhost object data
$VMHostobj = Get-VMHost $VMHost

#create vmlist
$vmlist = $VMHostobj | Get-VM

#Remove 1 uplink and migrate to standard
$VMhostObj | Get-VMHostNetworkAdapter -Physical -Name "vmnic1" | Remove-VDSwitchPhysicalNetworkAdapter -Confirm:$false
$VSwitch = $VMhostObj | Get-VirtualSwitch -Name $VSwitch

#Get physical adapter to move
$vmhostadapter = $VMhostObj | Get-VMHostNetworkAdapter -Physical -Name vmnic1 

# Get 

# Get Vmotion and Management Virtual Adaptesr
$vmk0 = Get-VMHostNetworkAdapter -vmhost $vmhostobj -name vmk0
$vmk1 = Get-VMHostNetworkAdapter -vmhost $vmhostobj -name vmk1

# Add 1 physical adapter 
Add-VirtualSwitchPhysicalNetworkAdapter -VirtualSwitch $VSwitch -VMHostVirtualNic $vmk0,$vmk1 -VMHostPhysicalNic $vmhostadapter -Confirm:$false

#Rename the VMkernel ports to Vmotion and Management
$VMhostObj | Get-VirtualPortGroup -Name "VMkernel" | Set-VirtualPortGroup -VLanId 20 -Name "Management Network"
$VMhostObj | Get-VirtualPortGroup -Name "VMkernel2" | Set-VirtualPortGroup -VLanId 10 -Name "VMotion"

# Loop through guests and set their networks.
foreach($vm in $vmlist){
    Get-NetworkAdapter $vm | %{
    Write-Host "Setting adapter" $_.NetworkName on $vm
    $_ | Set-NetworkAdapter -PortGroup (Get-VirtualPortGroup -VMhost  $VMHost -Standard -Name $_.NetworkName) -Confirm:$false
}
}

#Swing Second Physical Interface Over
$VMhostObj | Get-VMHostNetworkAdapter -Physical -Name "vmnic0" | Remove-VDSwitchPhysicalNetworkAdapter -Confirm:$false

$vmhostadapter = $VMhostObj | Get-VMHostNetworkAdapter -Physical -Name vmnic0
Add-VirtualSwitchPhysicalNetworkAdapter -VirtualSwitch $VSwitch -VMHostPhysicalNic $vmhostadapter -Confirm:$false

#Remove from VDS
Get-VDSwitch $VDSwitch | Remove-VDSwitchVMHost -VMHost $VMhostObj -Confirm:$false


Migrate Host and Guests from SS to VDS using Powercli 5.5

I have a pretty popular older post to migrate off of a VDS to a standard switch using some of the API calls, but this has now been made easier with Powercli 5.5. The addition and update of a few cmdlets around standard switch and VDS migrations are now here!

Requirements: Powercli 5.5

These are:

Remove-VirtualSwitchPhysicalNetworkAdapter

Add-VDSwitchPhysicalNetworkAdapter

Add-VirtualSwitchPhysicalNetworkAdapter

The script below will show how to move a single host and all of it’s guests to a VDS for a host with 2 physical uplinks.¬† It can be modified if you have more than 2 uplinks.¬† You will need to be sure to modify the commands to your environment like the line to migrate vmk0 and vmk1 make sure they line up with what you named your management and Vmotion port group!

In the next post, I will document how to go in the other direction to get off of the VDS to the Standard Switch!

Here is the script, feel free to use and distribute! This script assumes you already have a VDS setup with Virtual Machine portgroups with the same name and VLAN ID’s as on the standard switch already configured and setup. This allows the for loop in the script to loop through each interface and set the network label.

$VMHost = Read-Host "Enter Hostname to Migrate"
$VDSwitch = Read-Host "Enter VDSwitch Name"

#Get VMhost object data
$VMHostobj = Get-VMHost $VMHost

#create vmlist
$vmlist = $VMHostobj | Get-VM

#Add to new DVSwitch and swing leg over
$VMhostObj | Get-VMHostNetworkAdapter -Physical -Name "vmnic1" | Remove-VirtualSwitchPhysicalNetworkAdapter -Confirm:$false
$VDSwitch = Get-VDSwitch $VDSwitch
$VDSwitch |  Add-VDSwitchVMHost -VMHost $VMHost -Confirm:$false

#Get physical adapter to move
$vmhostadapter = $VMhostObj | Get-VMHostNetworkAdapter -Physical -Name vmnic1 

# Get Vmotion and Management Virtual Adaptesr
$vmk0 = Get-VMHostNetworkAdapter -vmhost $vmhostobj -name vmk0
$vmk1 = Get-VMHostNetworkAdapter -vmhost $vmhostobj -name vmk1

# Add 1 physical adapter 
Add-VDSwitchPhysicalNetworkAdapter -DistributedSwitch $VDSwitch -VirtualNicPortgroup "Management","Vmotion" -VMHostVirtualNic $vmk0,$vmk1 -VMHostPhysicalNic $vmhostadapter -Confirm:$false

# Loop through guests and set their networks.
foreach($vm in $vmlist){
    Get-NetworkAdapter $vm | %{
    Write-Host "Setting adapter" $_.NetworkName on $vm
    $_ | Set-NetworkAdapter -PortGroup (Get-VDPortGroup -Name $_.NetworkName -VDSwitch $VDSwitch) -Confirm:$false
}
}

#Swing Second Physical Interface Over
$VMhostObj | Get-VMHostNetworkAdapter -Physical -Name "vmnic0" | Remove-VirtualSwitchPhysicalNetworkAdapter -Confirm:$false

$vmhostadapter = $VMhostObj | Get-VMHostNetworkAdapter -Physical -Name vmnic0
$VDSwitch | Add-VDSwitchPhysicalNetworkAdapter -VMHostNetworkAdapter $vmhostadapter -Confirm:$false