Automation‎ > ‎

Scripts to migrate vCenter5.0 to a new vCenter 5.5 LIVE!

posted Jun 3, 2014, 9:31 AM by Kris Knight   [ updated Jun 14, 2016, 11:46 AM ]
So this was just an idea to migrate a 5.0 environment to a new 5.5 vCenter instance LIVE!

The idea: What if you want to upgrade to 5.5 without using the in-place upgrade instead standing up a new vCenter 5.5 instance and moving the hosts to it without downtime. I'm assuming the network can withstand the mass re-ARP of all the VMs from portgroup to portgroup. This has been tested in small scale virtual lab.

This assumes you have 2 vmnics on a Distributed vSwitch(DVS). This also assumes you have ur DVS configured for the same vmnics on each host. It's much easier without DVS, but it wouldn't be fun if it was easy.

I haven't quite ironed out SDRS Clusters yet, this will probably need to be reconfigured manually until I can figure this out.


1.) Export and Import the current DVS and DRS Rules to the new 5.5 Instance. Create the Clusters ahead of time.

2.) Move 1 vmnic to a standard vSwitch with all DvSwitch portgroups on it and migrate all VMs to the associated vSwitch portgroup.

3.) Drop the hosts from vCenter5.0 and reattach the hosts to vCenter 5.5

4.) Attach all the VMs to the new vCenter 5.5 distributed virtual switch portgroups and migrate the vmnic to the Distributed Virtual Switch.

5.) Export and Import VM Folders and move VMs(Not sure if this is necessary, but I added it just in case.)

#####################################################################
#Migration from 5.0 to new 5.5 vCenter Deployment
#Export DvS Portgroups
#
# Version v1.0 JAN 2014 KnightUSN
#####################################################################
param(
    [parameter(Mandatory = $true)]
    [string[]]$Cluster   
    )

$Export = "D:\Scripts\DVS.csv"

Get-Cluster $Cluster | Get-VMHost | Get-VDSwitch | Get-VDPortGroup | ?{$_.VlanConfiguration.VlanType -ne "Trunk"} | Select Name,NumPorts,VDSwitch,@{N="Vlan";E={$_.VlanConfiguration.VlanId}} | Export-CSV $Export -UseCulture -NoTypeInformation

#####################################################################
#Migration from 5.0 to new 5.5 vCenter Deployment
#Export DRS Rules
#
# Version v1.0 JAN 2014 
KnightUSN
#####################################################################
# Run .\DRS-RulesExport.ps1 "Cluster Name"

#=========Force Parameter Entry==========
param(
    [parameter(mandatory = $true)]
    [string[]]$clusterName
    )
#========================================

$outfile = "D:\Scripts\DRS\Rules.txt"

# If File does not exist REM the below line
Remove-Item $outfile

$rules = get-cluster -Name $clusterName | Get-DrsRule

foreach($rule in $rules){
  $line = (Get-View -Id $rule.ClusterId).Name
  $line += ("," + $rule.Name + "," + $rule.Enabled + "," + $rule.KeepTogether)
  foreach($vmId in $rule.VMIds){
    $line += ("," + (Get-View -Id $vmId).Name)
  }
  $line | Out-File -Append $outfile
}



#####################################################################
#Migration from 5.0 to new 5.5 vCenter Deployment
#
#Import DvS
#
#**NOTE You must add the DvS and Cluster First
#
# Version v1.0 JAN 2014 
KnightUSN
#####################################################################

param(
    [parameter(Mandatory = $true)]
    [string[]]$Cluster   
    )

$Import = "D:\Scripts\DVS.csv"

$PGs = Import-CSV $Import -UseCulture

$VDSwitch = Get-Cluster $Cluster | Get-VMhost | Get-VDSwitch

#Add PGs to DvS
ForEach($PG in $PGs){$VDSwitch | New-VDPortgroup -Name $PG.Name -NumPorts $PG.NumPorts -VlanId $PG.Vlan}


#####################################################################
#Migration from 5.0 to new 5.5 vCenter Deployment
#Cluster Prep for Host Migrations
#Creating Temporary vSwitch & Portgroups
#Migrate Uplink to SvS
#Migrating VMs on DvS to SvS
#
# Version v1.0 JAN 2014 
KnightUSN
#####################################################################

param(
    [parameter(Mandatory = $true)]
    [string[]]$Cluster
    )

#Name the Temporary vSwitch   
$TempVSwitch = "Temp-vSwitch"

#Identify vmnicX on hosts to migrate to SvS
$vNIC = "vmnicX"

#####################################################################

#Get Hosts
$cHosts = Get-Cluster $Cluster | Get-VMHost
   
#Create Temp vSwitch   
$cHosts | New-VirtualSwitch -Name $TempVSwitch -NumPorts 128

#Get Portgroups from DvS
$PGs = $cHosts | Get-VDSwitch | Get-VDPortGroup | ?{$_.VlanConfiguration.VlanType -ne "Trunk"}

#Create Portgroups on Temp vSwitch
ForEach($PG in $PGs){
    $TempName = $PG.Name + "VVV"
    $cHosts | Get-VirtualSwitch | ?{$_.Name -eq $TempVSwitch} | New-VirtualPortgroup -Name $TempName -VlanID $PG.VlanConfiguration.VlanId
}

#Migrate vmnic Uplinks from DvS to SvS
ForEach($cHost in $cHosts){
    $cHostNA = $cHost | Get-VMHostNetworkAdapter -Name $vNIC
    $cHostNA | Remove-VDSwitchPhysicalNetworkAdapter -confirm:$false
    $cHost | Get-VirtualSwitch -Name $TempVSwitch | Add-VirtualSwitchPhysicalNetworkAdapter $cHostNA -confirm:$false
}

#Get VM Network Adapters
$VMNAs = $cHosts | Get-VM | Get-NetworkAdapter

#Migrating VM Network Adapters to vSwitch
ForEach($VMNA in $VMNAs){
    $TempName = $VMNA.NetworkName + "VVV"
    Set-NetworkAdapter $VMNA -NetworkName $TempName -confirm:$false
}

#####################################################################
################ Remove DvS From Host         #######################
################ Remove Host From vCenter 5.0 #######################
################ Add Host to New vCenter 5.5 Cluster  #######################
#####################################################################



#####################################################################
#Migration from 5.0 to new 5.5 vCenter Deployment
#Migrate Host and VMs to DvS
#
#Host must be registerd in vCenter, DvS, and Portgroups Created
#
#Migrating VMs on SvS to DvS
#Migrate Uplink to DvS
#
# Version v1.0 JAN 2014 
KnightUSN
#####################################################################

param(
    [parameter(Mandatory = $true)]
    [string[]]$Host
    )
$gHost = Get-VMHost $Host

#Identify Temporary vSwitch on Host   
$TempVSwitch = "Temp-vSwitch"
$HvSwitch = $gHost | Get-VirtualSwitch | ?{$_.Name -eq $Temp-vSwitch}

#Identify DvS Name for VM
$DvSwitchName = "dvSwitchNAME"
$DvSwitch = Get-VDSwitch | ?{$_.Name -eq $DvSwitchName}

#Identify vmnicX For DVS
$vNICdvs = "vmnicX"
$gHostNAdvs = $gHost | Get-VMHostNetworkAdapter -Name $vNICdvs

#Identify vmnicX For current SvS
$vNICsvs = "vmnixX"
$gHostNAsvs = $gHost | Get-VMHostNetworkAdapter -Name $vNICsvs

#####################################################################

#Add Host Uplink to DvS
$DvSwitch | Add-VDSwitchPhysicalNetworkAdapter -Name $gHostNAdvs -confirm:$false

#Get VM Network Adapters and Migrate to DvS Portgroups
$VMNAs = $gHost | Get-VM | Get-NetworkAdapter
ForEach($VMNA in $VMNAs){Set-NetworkAdapter $VMNA -NetworkName $VMNA.NetworkName.Trim("VVV")}

#Remove Uplink from SvS
$hvSwitch | Remove-VirtualSwitchPhysicalNetworkAdapter $gHostNAsvs -confirm:$false

#Add Host Uplink to DvS
$DvSwitch | Add-VDSwitchPhysicalNetworkAdapter -Name $gHostNAsvs -confirm:$false

#Delete vSwitch
$HvSwitch | Remove-VirtualSwitch -confirm:$false

####################################################################
#Migration from 5.0 to new 5.5 vCenter Deployment
#Import DRS Rules
#
# Version v1.0 JAN 2014 
KnightUSN
#####################################################################
# The Cluster is defiened in the Rules.txt
$file = "D:\Scripts\DRS\Rules.txt"
$rules = Get-Content $file

foreach($rule in $rules){
  $ruleArr = $rule.Split(",")
  if($ruleArr[2] -eq "True"){$rEnabled = $true} else {$rEnabled = $false}
  if($ruleArr[3] -eq "True"){$rTogether = $true} else {$rTogether = $false}
  get-cluster $ruleArr[0] | `
    New-DrsRule -Name $ruleArr[1] -Enabled $rEnabled -KeepTogether $rTogether -VM (Get-VM -Name ($ruleArr[4..($ruleArr.Count - 1)]))
}


#####################################################################
#Migration from 5.0 to new 5.5 vCenter Deployment
#Export VM Folders and Custom Annotations
#
#Thanks LucD
#####################################################################

#Export the VMs and the folderstructure.
#####################################################################
 param(
    [parameter(Mandatory = $true)]
    [string[]]$Datacenter
    )

New-VIProperty -Name 'BlueFolderPath' -ObjectType 'VirtualMachine' -Value {
    param($vm)

    function Get-ParentName{
        param($object)

        if($object.Folder){
            $blue = Get-ParentName $object.Folder
            $name = $object.Folder.Name
        }
        elseif($object.Parent -and $object.Parent.GetType().Name -like "Folder*"){
            $blue = Get-ParentName $object.Parent
            $name = $object.Parent.Name
        }
        elseif($object.ParentFolder){
            $blue = Get-ParentName $object.ParentFolder
            $name = $object.ParentFolder.Name
        }
        if("vm","Datacenters" -notcontains $name){
            $blue + "/" + $name
        }
        else{
            $blue
        }
    }

    (Get-ParentName $vm).Remove(0,1)
} -Force | Out-Null
$dcName = $Datacenter

Get-VM -Location (Get-Datacenter -Name $dcName) |
Select Name,BlueFolderPath |
Export-Csv "vm-folder.csv" -NoTypeInformation -UseCulture


#####################################################################
#Migration from 5.0 to new 5.5 vCenter Deployment
#Import VM Folders and Custom Annotations
#
# Thanks LucD
#####################################################################

#The script uses a New-VIProperty to fetch the blue folderpath for a VM.

#Manually configure .CSV Location

#Import the folder structure and move existing VMs.

#####################################################################
param(
    [parameter(Mandatory = $true)]
    [string[]]$newDatacenter
    )

$newFolder = "Imported Folder"
$csvLocation = "C:\vm-folder.csv"

$startFolder = New-Folder -Name $newFolder -Location (Get-Folder -Name vm -Location (Get-Datacenter -Name $newDatacenter))

Import-Csv $csvLocation -UseCulture | %{
    $location = $startFolder
    $_.BlueFolderPath.TrimStart('/').Split('/') | %{
        $tgtFolder = Get-Folder -Name $_ -Location $location -ErrorAction SilentlyContinue
        if(!$tgtFolder){
            $location = New-Folder -Name $_ -Location $location
        }
        else{
            $location = $tgtFolder
        }
    }
   
    $vm = Get-VM -Name $_.Name -ErrorAction SilentlyContinue
    if($vm){
        Move-VM -VM $vm -Destination $location -Confirm:$false
    }
}

Comments