How to move a VM within VMs and Templates view using PowerCLI?

1.5k views Asked by At

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.

1

There are 1 answers

1
Brandon McBride On

There might be an easier way to create a folder in the VM and Templates view than above, but I just added:

$newVMFolder = Get-Folder -Name $newFolderName -Type VM

after I create the folder.

Now $newVMFolder is an inventory type instead of a managed object so

Move-VM -VM $vm -Destination $newVMFolder

now moves it into that folder.