Get off my array! (Scripting datastore migrations with PowerCLI)

6 05 2011

We were recently asked by our storage team to migrate all VMs off of one array we are using and onto another.  Not being a huge fan of planning and carrying out a move like this one virtual disk at a time, I decided to take a stab at a datastore evacuation script.  There was already some know-how out there, so I started poking around and found some useful posts & threads:

My main issue with existing scripts that I found was that they presumed the entire VM was on a single datastore – not the case in our environment.  In fact, virtual disks associated with a single VM are often on different tiers of storage for our customers.  I set out to do write this code with that in mind, and the result is a script that simply moves the vmdk’s that exist on one datastore to the destination, without touching anything I didn’t ask to be moved.

Also, giving credit where it’s due, I simply took Luc Dekens’ (LucD) code snippet from the VMware Communities thread on migrating a VM’s config files and plopped it into a function (migrateVMConfig) in my code.  Thanks Luc!

The current iteration of the migrateDatastore function checks for two criteria before proceeding with the move:

  • The destination datastore is equal in Capacity or larger than the source
  • The Free Space on the destination is greater than or equal to the Used Space on the source

The first check can probably be removed at some point, but the second is kind of a no-brainer – you can’t move actual used space to something smaller than it’s current datastore unless you are also going from thick to thin provisioned (which this script does not currently do).

I tested this script extensively in my dev environment before proceeding with any production moves, but please let me know if any bugs or problems crop up.  Along those lines, if you want to use this script please do your own testing before using it in production, and always start with one datastore at a time before progressing to large-scale evacuations.

Here’s the link to the code; as usual feel free to use it, steal it, change it, etc:

Usage (from the PS CLI):

.\MigrateDatastore.ps1 -VIServer "" -sourceDatastore "DS01" -destinationDatastore "DS02"



3 responses

12 10 2012

Excellent. Confirmed working at my place, a larger company in Norway. 🙂

30 11 2012

Thanks for this awesone script, very helpful! Only changed Set-Harddisk for Move-Harddisk to make it compatible with powercli 5.1

22 10 2014

it works only if you vsmotion the vm with the same format. you will need to specify the diskformat to change the disk type, especially if vsmotioning the rdm disk to the thikprovisionedzero disk. so the command is incomplete 😦

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: