简体   繁体   中英

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

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:

$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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM