VMware Cloud Community
dlm1975
Contributor
Contributor

Move VM to a different directory inside the datastore or NFS mount

I've been trying different combinations of searches and haven't yet hit upon the right answer.

What I want to do is create a PS script that will move a VM's folder, i.e., "[datastore1]/mytestvm" to a different directory other than root, i.e., "[datastore1]/Test Machines/mytestvm".

Is this possible?

Reply
0 Kudos
9 Replies
DSTAVERT
Immortal
Immortal

Moving to the PowerCLI forum.

-- David -- VMware Communities Moderator
Reply
0 Kudos
dlm1975
Contributor
Contributor

Note: I know of the Move-Folder cmdlet but this only moves a VM from one vSphere folder to another vSphere folder. Not the folder within the datastore or NFS mount. Correct me if I'm wrong.

Reply
0 Kudos
LucD
Leadership
Leadership

You can have a look at the Copy-DatastoreItem cmdlet.

But if you move a VM like this you will also have to take care of unregistering (Remove-VM) and registering (New-VM) the VM.


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

Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership

You are right. I first misunderstood your question and thought you were talking about the blue folders. After posting I saw that I was wrong and deleted my post.

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
dlm1975
Contributor
Contributor

Thanks for the tip.

Here's what I came up with. It removes 'mytestvm' from Inventory.

Copies 'mytestvm' from location [datastore1]\mytestvm\ to a folder called 'MyTestFolder', i.e., New location = [datastore1]\MyTestFolder\mytestvm.

My vSphere host is 'nydclabvm1'.

Remove-Inventory mytestvm
$ds = Get-Datastore datastore1

New-PSDrive -Name DS -PSProvider ViMdatastore -Root '\' -location $ds
Copy-DatastoreItem DS:\mytestvm\* -Destination MyDS:\MyTestFolder\mytestvm
cd vmstores:\nydclabvm1@443\ha-datacenter\datastore1\MyTestFolder\mytestvm\
$vmxFile = Get-Item *.vmx
New-VM -VMHost nydclabvm1 -VMFilePath $vmxFile.DatastoreFullPath

It works pretty well. I just need to script deleting the original VM.

This is basically a work-around. I would REALLY like to P2V a VM into a specific directory in my datastore, i.e., [datastore1]\MyTestFolder\mytestvm - but you're only able to choose the datastore when you P2V, not any specific directory.

I'll keep working on this and post my final version.

Reply
0 Kudos
avlieshout
VMware Employee
VMware Employee

What's the use case if I may ask?

Let vCenter handle directories, why bother.

If you perform a storage vMotion directories are changed according to the VM's name in vCenter anyway.

Arnim van Lieshout Blogging: http://www.van-lieshout.com Twitter: http://www.twitter.com/avlieshout If you find this information useful, please award points for "correct" or "helpful".
Reply
0 Kudos
admin
Immortal
Immortal

Hello,

Datastore provider allows copying, moving and removing folders within a datastore. Actually you can copy and move files between datastores from different datacenters located in same VC server. You can do these actions using the standard powershell commandlets copy-item, move-item, remove-item. Also you can create folders using New-Item or its alias mkdir.

Copy-DatastoreItem is commandlet which main purpose is to allow copying files from your local machine to a datastore or vice-versa. Copy-DatastoreItem also can be used instead of copy-item commandlet.

For example you can do something like this:

# getting the datastore where you want to operate

$ds = Get-Datastore <datastore name>

# putting the current location in to the root of the datastore

cd $ds.DatastoreBrowserPath

# unregister the VM

Remove-VM -vm $vm -Confirm:$false

# move the VM folder

mv .\MyVM .\TestVirtualMachines\

# register the VM back in to the inventory

$vmx = get-item .\TestVirtualMachines\MyVM\MyVM.vmx

New-VM -Name MyVM -VMHost <host name> -VMFilePath $vmx.DatastoreFullPath

In your scneario to remove the source folder contents you can use Remove-Item (rm) commandlet:

rm .\MyVM\

Vitali

PowerCLI Team

Reply
0 Kudos
dlm1975
Contributor
Contributor

Performing one-off test with NFS. Backup and restore. And my backup admins (Commvault) tell me that it's a faster to restore if instead of having all the VMs under the root directory; instead create subdirectories, i.e., "VM group 1", "VM group 2", etc...so that they can restore subdirectories as multiple jobs.

Reply
0 Kudos
dlm1975
Contributor
Contributor

Thanks! I will test this out right away and post my findings.

Reply
0 Kudos