Easy Migration Tool Using PowerCLI

Easy Migration Tool Using PowerCLI

VMotion and Storage VMotion are the two major features that are most heavily used in VMware and they both work perfectly well. However, as many of you already know (and are probably complaining about), it is a real pain to manually VMotion or SVMotion virtual machines. Here are the reasons:

  1. You have to know in which resource pools the VMs are.


  2. You cannot migrate the VMs all at once because you can only move them to one Resource Pool at a time.


  3. You have to make a million mouse clicks to migrate the VMs. This process can be very time consuming.


I decided to do some research on the Internet to find a solution and I came across the “Fast vMotion script with GUI” using PowerCLI by Arne Fokkema. The link is below:

http://ict-freak.nl/2009/07/21/powercli-fast-vmotion-script-with-gui/

It is a very nice script which provides you with a GUI to VMotion multiple VMs at the same time to a destination host without having to specify the resource pools. I’ve used it a few times at work and it was very helpful. Then I thought that it would be good if I can enhance the script and add a few more features to it like SVMotion. At work, we are in the process of changing storage vendor (HDS to EMC). As part of this project, we will have to SVMotion over 200 VMs onto the new datastores. I cannot imagine the amount of time (and mouse clicks) it will require if we have to use the vCenter console to do the migration. To resolve this issue, I’ve modified Arne’s script to create the Easy Migration Script for VMware. Below is a screenshot of the GUI.

EasyMigration.JPG

The Easy Migration script works on a per cluster level. This means that you will only be able to migrate VMs within the same cluster.

The Easy Migration script provides the following features:

  • Host-to-Host: Migrate all VMs from a source host to a destination host (VMotion)


  • VM-to-Host: Migrate selected VMs (from any host) to a destination host (VMotion)


  • Datastore-to-Datastore: Migrate all VMs from a source datastore to a destination datastore (SVMotion)


  • VM-to-Datastore: Migrate selected VMs (from any datastore) to a destination datastore (SVMotion)


  • Ability to undo selected tasks


  • Ability to undo ALL tasks


  • Save all tasks to a log file


  • Reload tasks from a log file


All successful migrations will be displayed in the Recent Tasks window. Whenever a VM has not been migrated, it will be shown in the Messages window.

Two big advantages of using the Easy Migration script are that you don’t need to know in which resource pools the VMs are residing and it reduces the amount of mouse clicks very considerably as compared to using the vCenter console. As a result, it will be very EASY for you to migrate VMs and will save you heaps of time.

The next sections will give you more details about the different features that I listed above.

Host-to-Host Migration

The host-to-host migration allows you to VMotion all the VMs on a source host to a destination host at the same time without the need for you to know which resource pools they are in. Note that only one source host and one destination host can be selected. Using vCenter, you would probably need to run the Migration wizard several times because you can only select one destination resource pool at a time. With the host-to-host migration, it is as simple as selecting the source host and the destination host. Click on the Migrate button and all the VMs will be VMotioned. The VMs will automatically be put into their respective resource pools.

To perform a host-to-host migration, follow the steps below:

  1. Open vSphere PowerCLI and browse to the directory where you saved the script.


  2. Type .\Easy_Migration.ps1 at the command prompt. This will load the Easy Migration form. (Note: I would recommend that you put the form next to the PowerCLI console window so that you can see any errors generated by the script and so that you can see the progress when you are migrating VMs.)


  3. Type the vCenter Server name in the vCenter Server box and click on Login.


  4. Select the cluster and click on the Host to Host button, which will be highlighted to show you the type of migration that you are currently doing.


  5. Select the Source host and click on the Source button.


  6. Select the Destination host and click on the Destination button. (Note: All the hosts in the cluster will be displayed except the Source host that you selected in the previous step.)Click on the Migrate button to start migrating the VMs.


VM-to-Host Migration

The VM-to-Host Migration allows you to VMotion selected VMs to a destination host. The VMs do not have to be on the same source host. Once again, there is no need to know the resource pools. Simply select the VMs and the destination host, and then click on the Migrate button. The VMs will automatically be put into their respective resource pools.

If you try to VMotion a VM to the same host, the following message will be displayed in the Messages window:

"Cannot Migrate Server A because the source host and the destination host are the same."

To perform a VM-to-Host migration, follow the steps below:

  1. Open vSphere PowerCLI and browse to the directory where you saved the script.


  2. Type .\Easy_Migration.ps1 at the command prompt. This will load the Easy Migration form. (Note: I would recommend that you put the form next to the PowerCLI console window so that you can see any errors generated by the script and so that you can see the progress when you are migrating VMs.)


  3. Type the vCenter Server name in the vCenter Server box and click on Login.


  4. Select the cluster and click on the VM to Host button, which will be highlighted to show you the type of migration that you are currently doing.


  5. Select one or more VMs and click on the Source button.


  6. Select the Destination host and click on the Destination button.


  7. Click on the Migrate button to start migrating the VMs.


Datastore-to-Datastore Migration

The datastore-to-datastore migration allows you to SVMotion all the VMs on one datastore to another at the same time without the need to know which resource pools they are in. Note that only one source datastore and one destination datastore can be selected. With the datastore-to-datastore migration, it is as simple as selecting the source datastore and the destination datastore. Click on the Migrate button and all the VMs will be SVMotioned. The VMs will automatically be put into their respective resource pools.

This feature is very useful if you need to migrate VMs from one storage platform to another, e.g. HDS to EMC or IP Storage to SAN.

There are a few assumptions for this type of migration:

  • A datastore is presented to the hosts within the same cluster.


  • All local storage and scratch partitions are excluded.


  • 20% of the datastore space is reserved for snapshots and logs.


The script only displays a list of datastores that are presented to the hosts in the selected cluster and that are used to store the VM files. The following line of code does that:

Get-Cluster -Name $ClusterName | Get-VMHost | Get-Datastore | Where-Object {$_.Name -notlike "*SCR01"} |

Where-Object {$_.Name -notlike "DAS*"} | % {$SourceDBListBox.items.add($_.Name)}

The Get-Cluster -Name $ClusterName | Get-VMHost | Get-Datastore statement lists the datastores for the selected cluster ($Clustername). The Where-Object {$_.Name -notlike "*SCR01"} statement excludes all the Scratch

partitions. The Where-Object {$_.Name -notlike "DAS*"} statement excludes all the local storage.

To perform a datastore-to-datastore migration, follow the steps below:

  1. Open vSphere PowerCLI and browse to the directory where you saved the script.


  2. Type .\Easy_Migration.ps1 at the command prompt. This will load the Easy Migration form. (Note: I would recommend that you put the form next to the PowerCLI console window so that you can see any errors generated by the script and so that you can see the progress when you are migrating VMs.)


  3. Type the vCenter Server name in the vCenter Server box and click on Login.


  4. Select the cluster and click on the Datastore to Datastore button, which will be highlighted to show you the type of migration that you are currently doing.


  5. Select the Source datastore and click on the Source button.


  6. Select the Destination datastore and click on the Destination button. (Note: All the datastores in the cluster will be displayed except the Source datastore that you selected in the previous step.)


  7. Click on the Migrate button to start migrating the VMs.


The script gets the size of the first VM and checks if there is enough available space on the destination datastore (Note: 20% of the destination datastore space will be reserved for snapshots and logs). If there is enough disk space, the VM will be SVMotioned and the script proceeds with the next VM. If there is not enough disk space, the following message will be displayed in the Messages window:

"Cannot migrate vm1 (Size = x GB) because there is insufficient disk space on Datastore1 (Available = y GB)."

The script will try to SVMotion as many VMs as possible to the destination datastore. If not all VMs have been SVMotioned, you can select another destination datastore and run the migration again. You can keep doing that

until all the VMs have been migrated. You will then get the following message in the Messages window:

"No VM is present on the source datastore Datastore1."

The script also verifies if there are any existing snapshots for each VM. This has been suggested by Earl Maud. So thank you Earl for the great idea. If there is a snapshot, the following message will be displayed in the Messages window:

"Cannot migrate vm1 because it has one or more snapshots."

By using the datastore-to-datastore migration, you don’t have to calculate how much disk space is available on the destination datastore. The script will just do it. You just need to select a source datastore and a destination datastore, and click on the Migrate button. If the script cannot migrate the VMs from the source datastore because of a lack of disk space, simply pick another destination datastore and it will try to move the VMs again. It’s EASY!

VM-to-Datastore Migration

The VM-to-Datastore Migration allows you to SVMotion selected VMs to a destination datastore. The VMs do not have to be on the same source datastore. There is no need to know the resource pools. Simply select the VMs and the

destination datastore, and then click on the Migrate button. The VMs will automatically be put into their respective resource pools.

If you try to SVMotion a VM to the same datastore, the following message will be displayed in the Messages window:

"Cannot Migrate Server A because the source datastore and the destination datastore are the same."

To perform a VM-to-Datastore migration, follow the steps below:

  1. Open vSphere PowerCLI and browse to the directory where you saved the script.


  2. Type .\Easy_Migration.ps1 at the command prompt. This will load theEasy Migration form. (Note: I would recommend that you put the form next to the PowerCLI console window so that you can see any errors generated by the script and so that you can see the progress when you are migrating VMs.)


  3. Type the vCenter Server name in the vCenter Server box and click on Login.


  4. Select the cluster and click on the VM to Datastore button, which will be highlighted to show you the type of migration that you are currently doing.


  5. Select one or more VMs and click on the Source button.


  6. Select the Destination datastore and click on the Destination button.


  7. Click on the Migrate button to start migrating the VMs.


Similarly to the datastore-to-datastore migration, during the VM-to-datastore migration, the script gets the size of the first VM and checks if there is enough available space on the destination datastore (Note: 20% of the destination datastore space will be reserved for snapshots and logs). If there is enough disk space, the VM will be SVMotioned and the script proceeds with the next VM. If there is not enough disk space, the following message will be displayed in the Messages window:

"Cannot migrate vm1 (Size = x GB) because there is insufficient disk space on Datastore1 (Available = y GB)."

The script also verifies if there are any existing snapshots for each VM. If there is a snapshot, the following message will be displayed in the Messages window:

"Cannot migrate vm1 because it has one or more snapshots."

By using the VM-to-datastore migration, you don’t have to calculate how much disk space is available on the destination datastore and you don’t have to know the resource pools. The script will just move the VMs and put them in their respective resource pools. If the destination datastore gets full, just select another destination datastore and run the migration again.

Undo Feature

When I was writing the Easy Migration script, I showed my work colleague my script and I asked him for his feedback. The first thing he asked me was “Can we put the VMs back to where they were originally?” The reason behind this question is that at work, we often get requests to put a host into maintenance mode. Because DRS is currently set to partially automated in our environment, we have to manually VMotion the VMs off the host. When maintenance is done and the host needs to go back online, we have to move the VMs back to that host. That’s how I came up with the Undo feature.

I added two buttons on the form for the Undo feature – Undo and Undo ALL. With the Undo button, you can basically undo one or more tasks (Note: To select multiple tasks, use the Ctrl or Shift key). With the Undo ALL button, you can

undo everything in the Recent Tasks window. All undone tasks are removed from the Recent Tasks window.

Whenever you have migrated some VMs and you need to move them back to their original location, simply use the Undo feature. There is no need to select the VMs, hosts or datastores again. Select one or more tasks, and then click on the Undo button. Or just click on the Undo ALL button. It’s EASY!

Save Tasks

The "Save Tasks" button allows you to save all the tasks in the Recent Tasks window to a log file. You might want to keep the log file for auditing purposes or you might want to use it for the Reload Tasks feature. See the next section.

Reload Tasks

This feature basically allows you to reload previous tasks saved in a log file and to undo some or all of the tasks. This is particularly useful if, for example, one host has a hardware problem which requires a few days to be resolved due to parts not being available. So you can migrate all the VMs off that host, put the host into Maintenance Mode and save all the tasks to a log file. A few days later, once the hardware fault is fixed, you can reload the tasks and undo them all to put the VMs back onto the host. How EASY is that!

Attachments
Comments

Hi Richard,

I am trying to use your script to migrate severals VMs but I get this error allways "Cannot migrate vm1 (Size = x GB) because there is insufficient disk space on Datastore1 (Available = y GB)."

I tried also to with empty datastores and VMs without disks.

I beleive the cause is not the 20% free space for snapshot

Regards

Not sure why, but this script generates duplicate clusters, and vms in its windows

Version history
Revision #:
1 of 1
Last update:
‎12-09-2010 05:17 PM
Updated by: