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:

1.   Take a host and create all needed Vlans on a standard switch to use as a template for the rest of hosts either using the GUI or through powercli commandlets.

2.  Run Powercli command to create all required vlans on ESXi hosts with vSwitch0 (if you don’t have vSwitch0 on hosts you will need to also create this)

3.  Migrate one uplink to standard switch

4.  Migrate Vmotion and Management

5.  Migrate all guest networking in for loop.

6.  Migrate second uplink to standard switch

7.  Remove from VDS

Now for the code:

1. Run this code to single or all hosts to copy port groups from template host to your host to migrate. Modify the TEMPLATE_HOST section to get port groups. If you need to do a single host modify the get-cluster line to just get-vmhost and the hostname:

#cluster or single host
$vmhosts = get-cluster Cluster |get-vmhost | Sort Name
$portgroups = Get-vmhost TEMPLATE_HOST | Get-VirtualSwitch -Name vSwitch0 | Get-VirtualPortGroup
$vmhosts | %{ 
    $vmhost = $_
    $portgroups | %{ 
        $vmhost | get-virtualswitch -Name vSwitch0 | New-VirtualPortGroup -Name $_.Name -VLanId $_.VLanID
        }
    }

2. Now you will want to migrate an interface. First, we will connect to the vcenter, then prompt for hostname, get host object, create VMlist to migrate to standard, and then move 1 physical adapter to standard.

Connect-VIServer Vcenter 

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

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

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

#Remove Vmnic1 adapter from VDS
$VMhostObj | Get-VMHostNetworkAdapter -Physical -Name vmnic1 | Remove-VDSwitchPhysicalNetworkAdapter -Confirm:$false

#Wait till it is removed
Start-Sleep -S 5

#Add Physical nic to Standard Switch
Get-VirtualSwitch -VMhost $VMHost -name "vSwitch0" | Set-VirtualSwitch -Nic "vmnic1" -Confirm:$false

3. Next we have to get the network system ID to perform the VMotion/Management migration on. Then we will use SDK code obtained using Onyx to migrate Vmotion and Management. If vmotion and management were previously created yo will get red on the add port group part, but that is ok. You can always omit that portion as well. Be sure to modify the VLAN tags for your networks in the code below.

#Set network ID object for Standard Switch
$networkid = $VMHostObj.ExtenSionData.Configmanager.NetworkSystem

# ------- AddPortGroup ------- for VMotion

$portgrp = New-Object VMware.Vim.HostPortGroupSpec
$portgrp.name = "VMotion"
$portgrp.vlanId = 100
$portgrp.vswitchName = "vSwitch0"
$portgrp.policy = New-Object VMware.Vim.HostNetworkPolicy

$_this = Get-View -Id $networkid
$_this.AddPortGroup($portgrp)



# ------- UpdateVirtualNic ------- for Vmotion

$nic = New-Object VMware.Vim.HostVirtualNicSpec
$nic.portgroup = "VMotion"

$_this = Get-View -Id $networkid
$_this.UpdateVirtualNic("vmk1", $nic)

# ------- AddPortGroup ------- for Management

$portgrp = New-Object VMware.Vim.HostPortGroupSpec
$portgrp.name = "Management"
$portgrp.vlanId = 0
$portgrp.vswitchName = "vSwitch0"
$portgrp.policy = New-Object VMware.Vim.HostNetworkPolicy

$_this = Get-View -Id $networkid
$_this.AddPortGroup($portgrp)



# ------- UpdateVirtualNic ------- for Management

$nic = New-Object VMware.Vim.HostVirtualNicSpec
$nic.portgroup = "Management"

$_this = Get-View -Id $networkid
$_this.UpdateVirtualNic("vmk0", $nic)

4. Now that VMkernel ports are moved over we will loop through the guests and change their adapters to standard switch.

#Migrate Guest Network
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
}

5. Finally, remove 2nd uplink and remove from your VDS. Be sure to set your VDS name in the get-vdswitch command at the end.

#Remove from vdswitch
$VMhostObj | Get-VMHostNetworkAdapter -Physical -Name vmnic0 | Remove-VDSwitchPhysicalNetworkAdapter -Confirm:$false
Start-Sleep -s 5
Get-VirtualSwitch -VMHost $VMHost -name "vSwitch0" | Set-VirtualSwitch -Nic "vmnic0","vmnic1" -Confirm:$false
Get-VDSwitch | Where {$_.Name -eq "VDS"} | Remove-VDSwitchVMHost -VMhost $VMHost -Confirm:$false

Putting it all together! Here is the code in it’s entirety:

Connect-VIServer Vcenter

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

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

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

#Remove Vmnic1 adapter from VDS
$VMhostObj | Get-VMHostNetworkAdapter -Physical -Name vmnic1 | Remove-VDSwitchPhysicalNetworkAdapter -Confirm:$false

#Wait till it is removed
Start-Sleep -S 5

#Add Physical nic to Standard Switch
Get-VirtualSwitch -VMhost $VMHost -name "vSwitch0" | Set-VirtualSwitch -Nic "vmnic1" -Confirm:$false

#Set network ID object for Standard Switch
$networkid = $VMHostObj.ExtenSionData.Configmanager.NetworkSystem

# ------- AddPortGroup ------- for VMotion

$portgrp = New-Object VMware.Vim.HostPortGroupSpec
$portgrp.name = "VMotion"
$portgrp.vlanId = 100
$portgrp.vswitchName = "vSwitch0"
$portgrp.policy = New-Object VMware.Vim.HostNetworkPolicy

$_this = Get-View -Id $networkid
$_this.AddPortGroup($portgrp)



# ------- UpdateVirtualNic ------- for Vmotion

$nic = New-Object VMware.Vim.HostVirtualNicSpec
$nic.portgroup = "VMotion"

$_this = Get-View -Id $networkid
$_this.UpdateVirtualNic("vmk1", $nic)

# ------- AddPortGroup ------- for Management

$portgrp = New-Object VMware.Vim.HostPortGroupSpec
$portgrp.name = "Management"
$portgrp.vlanId = 0
$portgrp.vswitchName = "vSwitch0"
$portgrp.policy = New-Object VMware.Vim.HostNetworkPolicy

$_this = Get-View -Id $networkid
$_this.AddPortGroup($portgrp)



# ------- UpdateVirtualNic ------- for Management

$nic = New-Object VMware.Vim.HostVirtualNicSpec
$nic.portgroup = "Management"

$_this = Get-View -Id $networkid
$_this.UpdateVirtualNic("vmk0", $nic)


#Begin guest migration

#change network labels on straddle host
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
}
    
    
}

#Remove from vdswitch
$VMhostObj | Get-VMHostNetworkAdapter -Physical -Name vmnic0 | Remove-VDSwitchPhysicalNetworkAdapter -Confirm:$false
Start-Sleep -s 5
Get-VirtualSwitch -VMHost $VMHost -name "vSwitch0" | Set-VirtualSwitch -Nic "vmnic0","vmnic1" -Confirm:$false
Get-VDSwitch | Where {$_.Name -eq "VDS"} | Remove-VDSwitchVMHost -VMhost $VMHost -Confirm:$false
Advertisements

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

  1. Juan says:

    This has been a great help for me, thanks!

  2. Jon Lundfelt says:

    This is all pretty slick. I am incorporating it into code that we are using to change the IP address of our vCenter server, and ESX hosts, which requires additional steps as you can imagine. I can post the full script when done, but my immediate problem is that I cannot seem to get the lines for the VMKernel ports to work, e.g

    ——- AddPortGroup ——- for VMotion
    $portgrp = New-Object VMware.Vim.HostPortGroupSpec
    $portgrp.name = “VMotion”
    $portgrp.vlanId = 227
    $portgrp.vswitchName = “vSwitch1”
    $portgrp.policy = New-Object VMware.Vim.HostNetworkPolicy

    $_this = Get-View -Id $networkid
    $_this.AddPortGroup($portgrp)

    I get the following error;

    Exception calling “AddPortGroup” with “1” argument(s): “The object or item referred to could not be found.”
    At line:1 char:1
    + $_this.AddPortGroup($portgrp)
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : VimException

    I am wondering what the equivalent PowerCLI command is for Esxhost | Configuration | Networking | VDS | Manage Virtual Adapters | vml1 | Migrate to Virtual Switch.

    Looking into that now though…. Let me know if anyone is interested in any of the REIP ESX Host code. Thanks, Jon

  3. Jon Lundfelt says:

    Never mind, just saw you posted an article w/ PowerCLI 5.5 cmdlets. This worked fantastic, even though our evenvironment was different (UCS 4 Nics, – vSwitch0 (vmnic0,1) and VDS vmnic1,2) the only problem I had, was that I had to create the target vswitch ahead of time-

    New-VirtualSwitch -VMHost $vmhost -Name vswitch1 -NumPorts 128

    Then I could run-
    $VSwitch = $VMhostObj | Get-VirtualSwitch -Name “vSwitch1”
    Add-VirtualSwitchPhysicalNetworkAdapter -VirtualSwitch $VSwitch -VMHostVirtualNic $vmk1 -VMHostPhysicalNic $vmhostadapter

    Thanks,
    Jon

    • markdjones82 says:

      Jon, that is correct, the vswitch already has to be created for my code to run per my notes on the posting. I suppose I should put that code in there for those that aren’t sure how to complete that :). Glad the code helped, that is why I put this out there!

      I am also working on a UCS Central design post as we are implemting UCS ourselves.

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

%d bloggers like this: