Migration tools / scripts

Recently I was in the position where I could re-engineer a large existing environment.
To aid and speed up the process I created a few scripts.

Please do not simply use these scripts without understanding exactly what they will do or even better run in a test environment prior to running in production.

1. Taking a host from dvswitch to vswitch in order to move from cluster, vcenter to another.

The below script will ask you for a reference host and then will ask you to specify the dvswitch(s). It will then create a duplicate virtual switch prefaced with vsphere5_ on the same host and create all port groups that are contained in the dvswitch you specify. If you specify a wild card that has more than one dvswitch it will create a new vswitch for the each dvswitch. Once this is complete, you need to flick over a nic and you can then migrate virtual machines from the dvport group to the virtual port group. See step 2.

# Vsphere 5 Migration Script
# Aaron Kent
# 5-09-2013
#
#
# Ask for host to use as migration host
Get-vmhost | select Name
$migrationHost = Read-Host 'Please input Migration Host Name from list above - *01* will work if unique'
get-vdswitch | select Name
$migrationswitchname = Read-Host 'Please input wild character for switchs to import (can be single or multiple) - *vdswitch007* will work'

Get-View -ViewType DistributedVirtualSwitch | where {$_.name -like $migrationswitchname} | foreach {
$VDSwitchName = $_.Name
$VDSwitchMoRef = $_.MoRef
#Create virtual Switch on host specified above
New-virtualSwitch -VMHost $migrationHost -Name vsphere5_$VDSwitchName -NumPorts 1024 -Mtu 9000

# Get all Distrubuted Port Groups on Cluster with VlanId

Get-View -ViewType DistributedVirtualPortgroup | where {$_.Config.DistributedVirtualSwitch -eq $VDSwitchMoRef} | where {$_.Name -notlike "*Uplinks*"} | %{
$portgroupname = $_.Name
$portgroupVlanId = $_.Config.DefaultPortConfig.Vlan.VlanId

# Take all Distrubuted Port Groups gathered above and add Standard Port Group to new Standard switch
Write-Progress -Activity "Creating $portgroupname with vlan $portgroupVlanID" -Status "on $VDSwitchName"
write-host vsphere5_$VDSwitchName

Get-virtualSwitch $migrationHost -name vsphere5_$VDSwitchName |New-virtualportgroup -Name vs-$portgroupname -VlanId $portgroupVlanID
}
}
#Configure the network adapters for each Virtual Switch as required
#If a vswitch has two network adapters drop the second and place on the relative virtual switch
#Be cautious with lavvc004 and lavvc005 as they are a bit more complicated

Write-Host "Please configure the vmnics for the new virtual switches and press anykey to continue"
Write-Host "You are about to start moving vm's from the DV Switch to the new vphere 5 virtual switch press anykey to continue"

#$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

Write-Host "Make sure you have allocated at least one nic with the correct vlans to the new vsphere 5 switch."

2. Get virtual machines on host and migrate from dvswitch to virtual switch.
I cheat and use excel here.. just makes it easier and I like to double check the network name and mac address.

Sure it could be simplified and done in one powercli script but I am a bit paranoid sometimes…

2a.

get-vmhost XXX | get-vm | Get-NetworkAdapter | select parent, name, macaddress, networkname | sort parent | ft -au

2b. copy and past into excel from Column A (use deliminated and just remove the extra seporators on name column)

In column E, label E1 as NewNetworkName

In E2 paste the following

 ="vs-"&D2 

Now you should have a the old network name with a vs- (for virtual switch) in front.

In Column F paste the following.

 ="get-vm " & A2 & " | get-networkadapter -name " & """" & B2 & """" & " | where {$_.macaddress -like " & """" & C2 & """" & "} | set-networkadapter -networkname " & E2 & " -confirm:$false"  

If you now copy F2 to notepad or notepad++ you will get the full gist of the command.

This will get the vm and check the networkname (where statement), then check the mac address matches (second where statement) and change the vlan from the old to the new name.

Once comfortable with the command you can copy the entire F2 – F200 for example and paste into powercli.

This will then free up all dvportgroups on the host. From there you can drop the host for the distributed switches and you have a free host that can be disconnected without affecting running guests and reconnected to the new location whether it be a new vcenter, cluster or datastore. I mention cluster because this way you do not have to put the host in maintenance mode to move, depending on your HA resources available.

Advertisements