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


Advertisements

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

Migrate ESXi host and guests from 1000v / VDS to Standard Switch using Powercli

*UPDATE I HAVE UPDATED THIS WITH THE NEW POWERCLI 5.5 cmdlets here:*
Migrate Host and Guests VDS to SS using Powercli 5.5

On occasion, there is a need to migrate an entire host or even cluster from the VDS to a standard switch. We happen to do this frequently for assorted operations like an upgrade or moving to another VDS. So, I wrote a script combining some of my previous posts to put it all together.

Below are the configuration and the steps we would follow for our environment.  You may need to tweak the scripts slightly depending on how many interfaces that you have.

Our Configuration:

1.  Hosts are on the 1000v

2.  We only have 2, 10 GB uplinks/vmnics.  These are Dell half height blades.

3. No standard switch currently configured, but Vswitch0 is present.

4.  Only 1 VMkernel portgroup for Vmotion and Management respectively.

5.  Portgroup names are labeled identically the same (required for the for loop to work properly.

Steps to migrate to the standard switch:
Read more of this post

Migrate VMkernel Ports from 1000v or VDS to Standard Switch using Powercli

*UPDATE I HAVE UPDATED THIS WITH THE NEW POWERCLI 5.5 cmdlets here:*
Migrate Host and Guests VDS to SS using Powercli 5.5

On occasion, we have a need to be able to remove an ESXi host from our distributed switch to a standard switch. In order to do this you can go through the GUI and choose “Migrate” adapters shown in the screen shot below. The command below assumes that you already have at least 1 physical adapter on both the VDS and Standard Switch. That part can be scripted out as well to move one interface over first, migrate vmkernel ports, and then the other interface.

In my case we need to get hosts from a 4.x Vcenter to a 5.x Vcenter along with all the guests. I will detail this migration to get guests/hosts and script used in a later posting located here.

migrate_networking

This requires multiple clicks and can be tedious if you have multiple hosts that need to be done.  Currently, there is no way that I know of to migrate VMkernel networking from DVS to SS in powercli.  You can go the other direction using the new DVS cmdlet Set-VMHostNetworkAdapter and the port group option.

I wrote a cmdlet to migrate vmkernel ports from DVS to standard. Here it is and the usage:

MigrateDVSwitch-Adapter -VMHost host -Interface vmk1 -NetworkName Vmotion -VirtualSwitch vSwitch -Vlan 77

function MigrateDVSwitch-Adapter{
        #Specify Parameters, VMhostname, VMkernel interface, Network Name, Vlan ID, Standard Switch nameparam (
        [Parameter(Mandatory=$true)][string]$VMHost,
        [Parameter(Mandatory=$true)][string]$Interface,
        [Parameter(Mandatory=$true)][string]$NetworkName,
        [Parameter(Mandatory=$true)][int]$Vlan,
        [Parameter(Mandatory=$true)][string]$VirtualSwitch)

        #Get esxi object
        $VMHostobj = Get-VMHost $VMHost
	#Get Network ID
	$networkid = $VMHostObj.ExtenSionData.Configmanager.NetworkSystem

        # ------- AddPortGroup to Standard Switch
	$portgrp = New-Object VMware.Vim.HostPortGroupSpec
	$portgrp.name = $NetworkName
	$portgrp.vlanId = $Vlan
	$portgrp.vswitchName = $VirtualSwitch
	$portgrp.policy = New-Object VMware.Vim.HostNetworkPolicy
	$_this = Get-View-Id $networkid
	$_this.AddPortGroup($portgrp)

        # ------- UpdateVirtualNic ----Migrates the virtual interface to the standard switch
	$nic = New-Object VMware.Vim.HostVirtualNicSpec
	$nic.portgroup =$NetworkName
	$_this = Get-View-Id $networkid
	$_this.UpdateVirtualNic($Interface, $nic)