Registering and Unregistering Virtual Machines

When you create a virtual machine, it becomes part of the inventory (inside the folder from which you called the creation method by default), and it is registered. If you copy virtual machine files to relocate the virtual machine, or if you remove the files from the inventory using the vSphere Client, it becomes unregistered and unusable. You cannot power on a virtual machine that is not part of the inventory.

To restore the virtual machine to the inventory, and make it usable again, you can use the RegisterVM_Task method, defined in the Folder managed object. You can register the virtual machine to a host or to a resource pool. You can register the virtual machine as a template if you want to use it to clone other virtual machines from.

The ColdMigration.java sample illustrates both registering and reconfiguring a virtual machine. At the heart of the sample is the following call, which registers the virtual machine. Arguments include the virtual machine’s current folder, datastore path, and name, whether to register as a template, and the resource pool or host to register the machine in.

ManagedObjectReference taskmor = cb.getConnection().getService().registerVM_Task(
                  vmFolderMor,vmxPath,getVmName(),false,resourcePool,host);

After registration, the virtual machine takes its resources (CPU, memory, and so on) from the resource pool or host to which it is registered.

The RemoveManagedObject.java sample illustrates unregistering a virtual machine.