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 "myvcenter.mydomain.com" -sourceDatastore "DS01" -destinationDatastore "DS02"