PoshCode Logo PowerShell Code Repository

VMWare Quick Migration by jgrote 26 months ago
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/1535"></script>download | new post

Performs the functional equivalent of a Hyper-V Quick Migration by suspending a VM, moving it to a new host, and resuming it. This does not require vMotion licensing. It works by providing required VM objects via the pipeline or the second argument, and specifying the Destination host in the first argument, so you can use whatever query you want to build a list of VM’s to send to the command.
This is very useful for migrating virtual machines around in an environment that isn’t licensed for vMotion, such as vSphere Essentials Plus customers. It can even be used to quickly migrate virtual machines between clusters, assuming both clusters have access to the same LUNs. Note that VMotion rules still apply! (You need processor or EVC compatability between hosts as the CPU sets need to match).

Want to perform maintenance on a host and need to move the virtual machines off? Easy:
Get-VMHost myhost1 | Get-VM | QuickMigrate-VM myhost2

Does not work with the free ESXi (except accidentally with ESX 3.5u2) due to the VMWare APIs being disabled.

  1. #########################################
  2. #Name: VMWare Quick Migration Function
  3. #Author: Justin Grote <jgrote NOSPAMAT enpointe DOT com>
  4. #Credit: Inspired by Mike DiPetrillo's Quick Migration Script: http://www.mikedipetrillo.com/mikedvirtualization/2008/10/quick-migration-for-vmware-the-power-of-powershell.html
  5. #Version: 0.2
  6. #Last Revised: 15 Dec 2009
  7. #
  8. #Description: Performs the fucntional equivalent of a Hyper-V Quick Migration by suspending a VM,
  9. #       moving it to a new host, and resuming it. This does not require vMotion licensing.
  10. #       it works by providing required VM objects via the pipeline or the second argument,
  11. #       and specifying the Destination host in the first argument.
  12. #       The commeand accepts both text strings and VMHost objects for the VMHost Parameter
  13. #
  14.  
  15. #Prerequisites:
  16. #       Powershell v1
  17. #       VMWare PowerCLI 4.0 (May work with earlier version but not tested)
  18. #       VMWare Foundation Edition or Better (for API support). May work with VMWare Server 2 but not tested.
  19. #
  20. #Instructions to Install: Save this script and import into your session with, for example, . C:\temp\quickmigrate.ps1
  21. #       You can also include it in your PowerCLI profile to have it automatically included.
  22. #Command Usage: get-vm MyTestVM | Quick-MigrateVM "MyTestHost2"
  23. #########################################
  24. function QuickMigrate-VM {
  25.         PARAM(
  26.                 $NewVMHost = ""
  27.                 , [VMware.VimAutomation.Client20.VirtualMachineImpl]$VMsToMigrate = $null
  28.         )
  29.         BEGIN {
  30.                 if (!$NewVMHost){
  31.                         Write-Error "No Destination VMHost Specified. You must specify a host to migrate to. `n Example: Get-VM `"Test`" | QuickMigrate-VM `"Host1`""
  32.                         break
  33.                 }
  34.                 elseif ($VMsToMigrate) {
  35.                         Write-Output $InputObject | &($MyInvocation.InvocationName) -NewVMHost $newVMHost
  36.                 }
  37.                 else {
  38.                         #If NewVMHost was provided as a String, convert it into a VMHost Object.
  39.                         if ($NewVMHost.GetType().Name -eq "String") {
  40.                                 set-variable -name destinationVMHost -value (get-vmhost $NewVMHost) -scope 1
  41.                         }
  42.                         #Make sure that we have a VMHost object to work with.
  43.                         if (! $destinationVMHost -or (! $destinationVMHost.GetType().Name -eq "VMHostImpl")) {
  44.                                 write-error "Destination VM Host was not found or you provided the wrong object type. Please provide a VMHostImpl object or specify the fully qualified name of a VM Host"
  45.                                 break
  46.                         }
  47.                         write-host -fore white "===Begin Quick Migration==="
  48.                 }
  49.         }
  50.        
  51.         PROCESS {
  52.                 $step = 0
  53.                 $skip = $false
  54.                 #In the Event of an error, output the error, and skip the rest of the current item.
  55.                 #This is a workaround for the fact that "continue" doesn't work in a function process loop.
  56.                 trap {
  57.                         write-host -fore yellow "`tSKIPPED: "$_.Exception.Message
  58.                         set-variable -name skip -value ($true) -scope 1
  59.                         continue
  60.                 }
  61.                 $vmToMigrate = $_
  62.                
  63.                 ### Validation Checks
  64.                 if($_ -is [VMware.VimAutomation.Client20.VirtualMachineImpl]) {
  65.                         write-host -fore white "Quick Migrating $($vmToMigrate.Name) to $NewVMHost..."
  66.                 }
  67.                 else {
  68.                         throw "Object Passed was not a Virtual Machine object. Object must be of type VMware.VimAutomation.Client20.VirtualMachineImpl."
  69.                 }
  70.                 # Check for connected devices
  71.                 if (! $skip -and (get-cddrive $vmToMigrate).ConnectionState.Connected -ieq "TRUE") {
  72.                         throw "Connected CD Drive. Please disconnect and try again."
  73.                 }
  74.                 if (! $skip -and (get-floppydrive $vmToMigrate).ConnectionState.Connected -ieq "TRUE") {
  75.                         throw "Connected Floppy Drive. Please disconnect and try again."
  76.                 }
  77.                
  78.                 # Make sure the current VM Host and the Destination Host are different.
  79.                 $sourceVMHost = get-vmhost -vm $vmToMigrate
  80.                 if (! $skip -and ($sourceVMHost.Name -eq $destinationVMHost.Name)) {
  81.                         throw "Source and Destination Hosts are the same."
  82.                 }
  83.                
  84.                 ###Validation Complete, begin Migration
  85.                 if (! $skip) {
  86.                         $step++
  87.                         write-host -fore cyan "`t$($step). Suspending $($vmToMigrate.Name)..."
  88.                         $suspend = Suspend-VM -VM $vmToMigrate -confirm:$false
  89.                 }
  90.                 if (! $skip) {
  91.                         $step++
  92.                         write-host -fore cyan "`t($step). Moving $($vmToMigrate.Name) "to" $($destinationVMHost.Name)"
  93.                         $migrate = Get-VM $vmToMigrate | Move-VM -Destination $destinationVMHost
  94.                 }
  95.                 if (! $skip) {
  96.                         $step++
  97.                         write-host -fore cyan "`t($step). Resuming" $vmToMigrate.Name"..."
  98.                         $resume = Start-VM -VM $vmToMigrate
  99.                 }
  100.                 write-host -fore green "`tCOMPLETED"
  101.         }
  102.  
  103.                 END { write-host -fore white "===END Quick Migration===" }
  104. }

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me