I am reorganizing the Host and Clusters and VMs and Templates in vCenter. I want to do this automatically using a script. I can move the VMs out of resource pools and into new ones no problem using the Move-VM command, however I also need to move the VMs into new folders in the VMs and Templates view as well. I can create the folder I want to move it to using this command:
$newVMFolder = (Get-View (Get-View -ViewType Datacenter -Filter @{"name"=$vmFolderLocation}).vmfolder).CreateFolder($newFolderName)
Then when I use this command:
Move-VM -VM $vm -Destination $newVMFolder
I get this error, 'Cannot convert the "Folder-group-..." value of type "VMware.Vim.ManagedObjectReference" to type "VMware.VimAutomation.ViCore.Types.V1.Inventory.VIContainer".'
Any help would be much appreciated.
There might be an easier way to create a folder in the VM and Templates view than above, but I just added:
after I create the folder.
Now $newVMFolder is an inventory type instead of a managed object so
now moves it into that folder.