VMware Cloud Community
msuvanto_MNIT
Enthusiast
Enthusiast

Copying files and directories from one datastore to another ?

I have been searching for a while for very simple script to copy all folders and their content from one datastore to another datastore.

These are not VM's so vmotion is not an option as far as I know. These are OVA files and ISO files in their own folders and misc. files of that sort. No running VM's.

Found this:

Managing the VMware datastore with PowerCLI (PowerShell) – 4sysops
But ideally I would like this to run all without using PSDrive so my workstation is not involved in moves at all.

One would think this is an easy task but the more I search for a simple script the harder the problem gets.

Any suggestions on this?

Reply
0 Kudos
7 Replies
LucD
Leadership
Leadership

Not possible with a cmdlet I'm afraid.

But you can use the CopyDatastoreFile or the MoveDatastoreFile method.

Something like this for example

$isoName = 'MyFile.iso'

$srcDS = 'DS1'

$srcFolder = 'ISO1'

$dstDS = 'DS2'

$dstFolder = 'ISO2'


$fileMgr = Get-View FileManager


$src = "[$srcDS] $srcFolder/$isoName"

$srcDCMoRef = (Get-Datastore -Name $srcDS).DatacenterId

$dst = "[$dstDS] $dstFolder/$isoName"

$dstDCMoRef = (Get-Datastore -Name $dstDS).DatacenterId

$force = $true


$fileMgr.CopyDatastoreFile($src,$srcDCMoRef,$dst,$dstDCMoRef,$force)


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
msuvanto_MNIT
Enthusiast
Enthusiast

I would need to have the script to do that essentially one file at a time correct.

List the files and then a foreach loop? If I understand it correctly.

Reply
0 Kudos
LucD
Leadership
Leadership

If you give the path to a folder, the folder, and all files in it, will be copied (or moved).


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
msuvanto_MNIT
Enthusiast
Enthusiast

Very nice. I shall give it a shot.

Reply
0 Kudos
vmCalgary
Enthusiast
Enthusiast

LucD

Will the directory structure copy/build on the destination datastore exactly how it was on the source datastore?

Reply
0 Kudos
LucD
Leadership
Leadership

Yes, did you notice otherwise?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
vmCalgary
Enthusiast
Enthusiast

I have not noticed otherwise, @LUCD. I am preparing to move data from our old datastore that did not house VMs, only files such as ISOs and OVAs, to our new datastore for the same purpose. I was curious if I had to build the directory structure on new (destination) to match old (source). Your most recent answer says I don't need to build the hierachy on the new datastore, simply run your script. Smiley Happy

Thanks Luc. I will keep you posted if I have any issues.

Reply
0 Kudos