raymcdonald
Contributor
Contributor

Migrating distributed vSwitch to new vCenter

Jump to solution

Hi All,

So I have been using these great scripts from.

http://www.gabesvirtualworld.com/migrating-distributed-vswitch-to-new-vcenter/

They work great, but I need to know if I can change the value to Get-Cluster instead of Get-Datacenter, I am trying to run this against some clusters and the script runs against the datacenter level.

I will be doing some testing but I wanted to see what others have to say about this.

Thanks

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

You will have to disconnect those 4 ESXi hosts from the vdSwitch before migrating I suspect.

You can use the Remove-VDSwitchVMHost cmdlet for that.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
6 Replies
LucD
Leadership
Leadership

If you look at the Network Inventory in the vSPhere client, you'll notice that dvSwitches are defined in the hidden "network" folder that is in the root of each datacenter. A dvSwitch is defined on the level of the datacenter.

What you could do to limit it to a specific cluster, is look at which ESXi are connected to a specific dvSwitch. And from these ESXi you are able to retrieve the cluster these ESXi belong to.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
raymcdonald
Contributor
Contributor

How would I do that, using code, I am pretty new at PowerCLI.

0 Kudos
LucD
Leadership
Leadership

The following code should return the clusters that have hosts connected to a specific vdSwitch

$tgtVDS = "MyVDS"

$vds = Get-VDSwitch | where {$_.Name -eq $tgtVDS} $esxNames = @() $vds.ExtensionData.Config.Host | %{     $esxNames += Get-View $_.Config.Host | Select -ExpandProperty Name
} Get-VMHost -Name $esxNames | Get-Cluster | Select Name


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
raymcdonald
Contributor
Contributor

Well I already know which clusters are connected to the vDswitch, the issue is that all the hosts are connected to one vDswitch and I just want to migrate say four hosts in one cluster, will this still work?

Thanks

0 Kudos
LucD
Leadership
Leadership

You will have to disconnect those 4 ESXi hosts from the vdSwitch before migrating I suspect.

You can use the Remove-VDSwitchVMHost cmdlet for that.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
markdjones82
Expert
Expert

Ray,

  Not exactly what you are doing but might give you an example of how to move a host.  THis script I created will migrate our 2 interfaces from a DVswitch back to a standard and then reregister at a new Vcenter.

This will put host in maint mode, but you could remove that part and do a disconnect/remove instead after you've done your migrate VM's to standard switch.  I do hardcode the 2 vcenter names in a couple spots in the script where you would want to modify for your 2 vcenters. So you might have to tailor it to your needs.

You could do a txt list with all the host names example:

$hosts = get-content hostlist.txt

foreach ($vmhost in $hosts) {

   Migrate_Host -VMHost $VMhost -VDSwitch "switchname"

}

function Migrate_Host {
param (
[Parameter(Mandatory=$true)][string]$VMHost,
[Parameter(Mandatory=$true)][string]$VDSwitch)
 
#Get Credentials to pass to Connect
$credential = Get-Credential -Message "Please enter your credentials"

#Connect to old Vcenter
Connect-VIServer vcenter01 -Credential $credential

#Set host into maintenance mode
Set-VMHost -VMHost $VMHost -State Maintenance -Evacuate -Confirm:$false

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

#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 = 3721
$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)

#Remove 2nd nic from the VDS
$VMhostObj | Get-VMHostNetworkAdapter -Physical -Name vmnic0 | Remove-VDSwitchPhysicalNetworkAdapter -Confirm:$false

Start-Sleep -s 5

#Move both interfaces to standard switch
Get-VirtualSwitch -VMHost $VMHost -name "vSwitch0" | Set-VirtualSwitch -Nic "vmnic0","vmnic1" -Confirm:$false

#Remove host from VDswitch
Get-VDSwitch | Where {$_.Name -eq $VDSwitch} | Remove-VDSwitchVMHost -VMhost $VMHost -Confirm:$false


#If remove from VDS successful Remove host from Vcenter

if ($?) {
    Start-Sleep -s 5
    Remove-VMHost -VMHost $VMHost -Confirm:$false
    Disconnect-VIServer vcenter01 -Confirm:$false
    Start-Sleep -s 5
    #Connect to new Vcenter and add host to cluster
    Connect-VIServer vcenter02 -Credential $credential
    #add transport host
    Add-VMHost -Name $VMHost -Location "Cluster1" -User root -Password "pass"-Force -Confirm:$false
    }
   else {
    exit 
    } 
}

http://www.twitter.com/markdjones82 | http://nutzandbolts.wordpress.com