Myths, Misconceptions, Half-Truths and Lies about Virtualization Virtual Server Sprawl
Oct 01

Earlier this year VMware released the VMware Infrastructure Toolkit (for Windows). This was a new PowerShell interface for VMware Infrastructure 3. I’m a big fan of the power and simplicity of PowerShell so I thought I’d try to create something with the script and show it off during my talk at VMworld on Deploying VMware in a Microsoft Shop. The question is what do I create? Well, there has been some great debate of Microsoft’s Quick Migration and VMware’s VMotion and are they equal or not, do they solve the same problems, etc. This post isn’t to go back into that debate. One of the things that was also brought up over and over again in the debate was the fact that Quick Migration was free and came with Hyper-V since it was based on Microsoft Clustering whereas VMware VMotion was only available in the most expensive Enterprise SKU of VMware Infrastructure. So there it was – I need to create Quick Migration for the lower priced and free VMware solutions. I went off, built my little script, and showed it off at VMworld. There was a GREAT response to it so I’m posting it here for others to use or improve upon however you see fit.

The script works just like Microsoft Quick Migration – the virtual disk is stored on shared storage, the VM is suspended to disk, and the VM is then resumed on the destination. Presto! Here’s a quick video:

 

 

 

 

To set this up in your environment you’ll need a few things:

  1. 2 VMware Infrastructure 3 hosts
  2. Windows PowerShell (and all of the pre-requirements for that if needed)
  3. VMware Infrastructure Toolkit (for Windows)
  4. The VM Migrator script from me
  5. A VM you want to migrate – it doesn’t matter what the guest OS is

Once you’ve got all of that you’re ready to go. Assuming you have Microsoft PowerShell installed and the VMware Toolkit installed on top of that you should be ready to run the script. PowerShell has some built-in security so you might have to either (a) answer yes when it prompts you to run unsigned scripts, (b) sign this script and make it yours, or (c) Set-ExecutionPolicy unrestricted. After you’ve got all of that sorted out just run the script by opening a new PowerShell window, changing to the path of the file, and running it using ./vmmigrator-1.0.ps1. The script will prompt you for the VirtualCenter address and an appropriate username and password to connect to it. You’ll next prompted for which VM you want to migrate and where you want to migrate it to. The script does some rudimentary checking for network, datastore, and connected devices before it will let you migrate.

Some last minute notes:

  • I’m not a developer or scripter by trade. This thing can probably be improved a lot. Feel free to do just that.
  • You can post the script or your modifications wherever you want. I just ask for a courtesy link back to here as well.
  • Unfortunately the free ESXi has a VERY limited API set enabled in the current 3.5 U2 release. This script won’t work with it. I’ve brought this to the product manager’s attention and I’m hoping we can open the API set up to everything shortly. Not really sure why it was restricted in the first place. For now you’ll need at least the Foundation SKU of Virtual Infrastructure.

Some future things I’d like to do with this:

  • I say the script requires shared storage right now but actually it doesn’t. The PowerShell cmdlet that does the migration actually does a “Relocate VM” which will move the storage for the VM from one internal datastore to another. That process is SLOW but it does work. Right now the script is hard coded to keep things on the same datastore. With some slight modifications it can do a Quick Migration with 2 standalone hosts. I’ll probably make a version 2 that will do that.
  • I’d like to integrate this into PowerGUI so you can just right-click on a VM in Virtual Center and run this script which will do a Quick Migration to a host of your choice. That project is a little further down my to-do list.

I hope you enjoy the script. Feel free to leave comments to let me know if this is useful or not.

No TweetBacks yet. (Be the first to Tweet this post)
VN:F [1.7.8_1020]
Rating: 0.0/5 (0 votes cast)
VN:F [1.7.8_1020]
Rating: 0 (from 0 votes)

Tags: , ,

  • glmrenard
    Hello,

    great script but unfortunately it doesn't work with me. I have to esx3.02 server with a shared storage but when I run the script, it only sees my host and not the other esx server, any idea.

    By the way, your script is great !
  • fabiodangelo
    Hi Mike,
    I'm very interested in your script but I've the same problem that had "glmrenard".
    The script works but it doesn't see the second esx server (v. 3.02).
    The two servers have to be in cluster?
    Have you some suggestions? (esx version, VMware Infrastructure Toolkit ver ?)
    Thanks for any help,
    Fabio
  • Mike,

    This is very cool and integrating the script as an Action inside PowerGUI is very easy:
    1. In PowerGUI, go to the Virtual Machines node.
    2. In the Actions pane, click Add new item / Script Action.
    3. Paste your code inside the loop:

    $input | foreach-object {

    $vmToMigrate = $_
    # your code here

    }

    4. Now modify a few things:
    Prompt for the target: e.g. add a param as the first line of the script:
    param($target)

    Get rid of the stuff you do not need:
    Loading the snapin, connecting to VIServer, prompting for source info, etc. - PowerGUI does that all for you!

    Very easy! And if you have any questions - the discussion forums at PowerGUI.org are there to help! :)

    Again, congratulations on a very cool script!

    Dmitry
  • Martin
    migrate all vm's option would be nice for hw/esx maintenance. and then moving same vm's back after the maintenance is complete.
  • Mike, thanks for the script.

    I felt free to change the script, so it fully integrates within PowerScripter.

    Doing an automatic disconnect of medias (if wanted) and showing a progress bar.

    So you can just right click on a vm within VirtualCenter and choose quick migrate.

    The script can be downloaded here:
    http://www.icomasoft.com/forum/showthread.php?p...
  • That's fast work, Dennis and awesome!!
  • Hi Mike

    Excellent script Mike, just been testing it out against our virtualcenter using an XP machine. Worked an absolute treat and dowtime is minimal about 1 - 1.5 minutes. I can see us using this in our development and test environments where we haven't bought vMotion licences but still occasionaly have the requirement to migrate machines between hosts.

    I see it hasn't taken long before the script has been amended. Will post a link on my blog back to this as this is great stuff.
  • David
    Hi

    Great script, but is there anyway of adjusting the port used to connect to virtual centre with? I have a few VC instances that use non default ports.

    I am not a powershell expert either...

    Keep it up

    Cheers
  • Malik
    Excellent script mike ,
    Can anybody tell me how much time is a VM taking to migrate as it takes long time for me. Is that normal????
  • GregW
    Mike - good work mate. Really flies in the face of M$
  • Hey, David. There is a way to prompt for the port. I'll add that to the list of things to change for the next script update. Unfortunately I'm a little slammed at work until the middle of the month so it might be a bit until I get there. Pretty much all you would have to do is edit the line that starts with VI-Connect and put the port number towards the end with the "-port" option. That would be the easiest way.
  • Malik,

    It shouldn't take but a few seconds to move the VM over. Of course that depends on several things:

    1) How much memory is in use in the VM. The more memory the longer it will take to suspend the VM to disk.

    2) The speed of your storage. If you have really slow storage then it will take a long time to suspend the memory of the VM to disk.

    Of course there might be something in your setup to where it's actually copying the vmdk from one datastore to another. If that's the case then it will take a VERY long time. It should check for that and just leave it on the same datastore. Of course the part of the script that breaks out the datastore name might be doing something strange and causing this behavior. What is the datastore name that the VM is running on?
  • Johannes
    Recently, I had to quickly move a few VMs on my ESX 3.5 (and I don't have a vmotion license). I evaluated my options and ended up using VMX : free, http://www.trilead.com/Products/VM_Explorer - so far quite easy to use.

    I did not use shared storage, rather I moved the (suspended) VMs/disk files directly to an ESX in another datacenter. The process was not slow, I was able to copy the VM's with about 30 MB/sec (gigabit link). I also tried the compressed transfer option, but I think this pays out only on slow links.
  • malik
    Mike,
    Thanks for the reply...

    My datastore name is Storage1
  • Frank
    nice script. What if my vmware Infrastructure client is a VM and i need to move it to another ESX host?? Can I do it with this script??
  • Frank,

    If your *client* is running in a VM you'll be fine. If your *VC server* is running in a VM then this won't work as written. I've had some requests to re-write the script so it talks directly to ESX hosts. Unfortunately I just haven't had the time to complete that yet.
  • Frank
    Mike,

    Thanks for the replay.
  • Markus
    Hi,
    thx for the script. That is what i need! But it doesnt work here. I have 2 ESX 3.5 U2 and VC-Server on Win2003 with PowerShell 1.0 and the VI Toolkit.
    Here is the result:

    Please select a VM from the list below to migrate.

    1) DEGTEST

    Select a VM: DEGTEST
    Method invocation failed because [System.String] doesn't contain a method named 'op_Subtraction'.
    At C:\vmware_skripts\quick_migrate\vmmigrator-1.0.ps1:36 char:40
    + $vmToMigrate = $sourceVMs[$vmselection-1 <<<< ]
    Get-CDDrive : Cannot validate argument because it is null.
    At C:\vmware_skripts\quick_migrate\vmmigrator-1.0.ps1:40 char:17
    + if ((get-cddrive <<<< $vmToMigrate).ConnectionState.Connected -ieq "TRUE") {
    Get-FloppyDrive : Cannot validate argument because it is null.
    At C:\vmware_skripts\quick_migrate\vmmigrator-1.0.ps1:45 char:21
    + if ((get-floppydrive <<<< $vmToMigrate).ConnectionState.Connected -ieq "TRUE") {
    Get-NetworkAdapter : Cannot validate argument because it is null.
    At C:\vmware_skripts\quick_migrate\vmmigrator-1.0.ps1:52 char:39
    + $sourcePortGroup = (Get-NetworkAdapter <<<< $vmToMigrate).NetworkName
    Get-HardDisk : Cannot validate argument because it is null.
    At C:\vmware_skripts\quick_migrate\vmmigrator-1.0.ps1:54 char:33
    + $sourceDatastore = (Get-HardDisk <<<< $vmToMigrate).Filename
    You cannot call a method on a null-valued expression.
    At C:\vmware_skripts\quick_migrate\vmmigrator-1.0.ps1:55 char:39
    + $stopString = $sourceDatastore.IndexOf( <<<< "]")
    You cannot call a method on a null-valued expression.
    At C:\vmware_skripts\quick_migrate\vmmigrator-1.0.ps1:56 char:46
    + $sourceDatastore = $sourceDatastore.Substring( <<<< 1,$stopString-1)
    Get-VMHost : The argument cannot be null or empty.
    At C:\vmware_skripts\quick_migrate\vmmigrator-1.0.ps1:62 char:29
    + $sourceHost = Get-VMHost -VM <<<< $vmToMigrate

    Please select a destination host from the list below.

    1) degesx02.klideg.de
    2) degesx01.klideg.de

    Select a host: 1

    The datastore for this VM does not exist on the destination host. Exiting.

    Hope you have any idea.

    Thx
    Markus
  • Luis Muñoz
    This problem happens when you have more than one Vdisk in your VM. You have to compare the array ($sourceDatastore.SyncRoot[$i].Filename) with $Datastores and create one semaphore array if every elements are -eq 1 You can pass the test. I'm in trance of develope it.
  • ViTxO
    Hi Mike,

    I am testing with virtual machines. I would like to test your script but the link does not work. You can tell me where to download?

    Thanks
  • Thanks for noticing the broken link. It's fixed. Try again and have fun with the script!
  • Peter Klingler
    1.) Have you had a chance to work on a "Version 2" of your quick migration script...to move the storage from one internal host storage to another internal host storage?

    2.) Have you created a script that works for PowerGUI?
  • I haven't had a chance to test on internal only storage yet. It shouldn't take much to change it around. I just haven't had any real lab time. I'll probably revisit this after VMworld Europe.

    I also don't have a version for PowerGUI although the nice people at Icomasoft updated the script to work with PowerScripter. It's a nice GUI integration. Check them out here: http://www.icomasoft.com/products/vi-powerscrip....
  • Peter Klingler
    Thanks for getting back with me. I will look forward to your updates on this script. For our environment the ability to move from internal storage to shared storage and back or directly from internal storage to internal storage would be helpful.

    Thanks again for your work.
  • justingrote
    Awesome script Mike! I've rewritten it as a function, so you can pipe VM objects to it via powerCLI, like this:

    get-vm -folder "MyVMs" | quickmigrate-vm "mynewhost"

    http://poshcode.org/1249
  • sangepu
    Great, this script really worth if a company don't want to spend on vmotion. Does anyone know how to migrate/move the VM directly from ESX without shutdown/restart
  • fabiodangelo
    Hi Sangepu,
    I have some problems using this script and I think that the problem is that I'm using the script with Virtual Infrastructure and not with VMware Virtual Center Server.
    Can I ask you with what application are you using the script?
    Thanks
    Fabio
  • sangepu
    hey....sorry for the late reply as i was on vacation.

    which script you are talking about ?
  • fabiodangelo
    Hi Sangepu,
    i'm talking about the script "Quick Migration for VMware", Mike's script.
    I've 2 esx server(3.0.1 e 3.0.2), I 've used the script invoking it on 1 of them(by its address), the script works but it doesn't see the second esx server (v. 3.02).
    For this reasom, I've tought that the 2 esx server have to be "joined" through a VMware Virtual Center Server.
    Thanks for any help,
    regards
    Fabio
blog comments powered by Disqus