Move vSphere Replicated VM files from one datastore to another

Recently at my day job we had some new storage allocated at our recovery site to use for vSphere storage.  I was tasked with decommissioning the old datastores.  The problem is that my replicated VMs resided on the old storage.  Of course I could go into my vSphere replication settings on each VM and just point it to the new datastores and be done with it.  That would have taken quite some time to do since the VMs would have to fully replicate again.  I wanted to find an easy way to copy the replicated VMs from the old datastores to the new datastores.  So I did some Internet searches and found the following blog post:  Copy Files Between Datastores – PowerCLI.   Dan Hayward posted a useful PowerCLI script that he used to copy ISO files from one datastore to another.  I basically adapted this script and changed it to move a VM from an old datastore to another.  I could have scripted it and passed in the variables from a CSV file but I wanted to update the vSphere Replication settings one VM at a time.  So here is what my script looked like:

Connect-VIServer ServerName

#Set’s Old Datastore
$oldds = get-datastore “OldDatastore”

#Set’s New Datastore
$newds = get-datastore “NewDatastore”

#Set’s VM Folder Location
$VMloc = “VMName”

#Map Drives
new-psdrive -Location $oldds -Name olddrive -PSProvider VimDatastore -Root “\”
new-psdrive -Location $newds -Name newdrive -PSProvider VimDatastore -Root “\”
#Copies Files from Old to New
copy-datastoreitem -recurse -force -item olddrive:\$VMloc\$VMloc*.vmdk newdrive:\$VMloc\

Basically the script connects you to your vCenter server, sets the old and new datastore variables, sets the VM Folder name and then does the magic to map the datastores and copy the VMDK files from the old to the new.  Having the VMDK files copied over to the new datastores allowed me to use these as my replication seed for each drive when I reconfigured replication settings for the VM.  I just updated this file for each VM that I needed to copy to the new datastores.

Obviously this could have been automated even more as I had to do this for over 120 VMs but I am not a scripting expert.  I am just thankful for a great blog post from Dan Hayward to help me out!  Thanks Dan!

  • Adrian Cristea

    I believe the VM has to be powered off when you do the files copy. Correct?

    • Adrian I honestly do not remember if they had to be powered off or not. I did this over 2 years ago at my previous job so the details are a bit scarce. Sorry.