使用vmware vsphere通过模板clone虚拟机器错误

使用vmware vsphere通过模板clone虚拟机器错误,报制定的参数错误。环境是集群,模板在其中一台主机上。
代码如下:
Map aa = getMOREFsInContainerByType(serviceContent.getRootFolder(),
"ResourcePool");
Set bb = aa.keySet();
String pool = null;
for( String poolName : bb)
{
pool = poolName;
break;
}

  ManagedObjectReference datacenterRef =
        vimPort.findByInventoryPath(serviceContent.getSearchIndex(),
                dataCenterName);

  if (datacenterRef == null) {
     System.out.printf("The specified datacenter [ %s ]is not found %n",
           dataCenterName);
     return;
  }
  // Find the virtual machine folder for this datacenter.
  ManagedObjectReference vmFolderRef =
        (ManagedObjectReference) getDynamicProperty(datacenterRef,
              "vmFolder");
  if (vmFolderRef == null) {
     System.out.println("The virtual machine is not found");
     return;
  }

// vimPort.markAsVirtualMachine(_this, pool, host);
ManagedObjectReference vmRef =
vimPort.findByInventoryPath(serviceContent.getSearchIndex(),
vmPathName);
//

if (vmRef == null) {
System.out.printf("The VMPath specified [ %s ] is not found %n",
vmPathName);
return;
}
ManagedObjectReference host =
getDecendentMoRef(null, "HostSystem","10.134.98.186");

  ManagedObjectReference hMOR = getHostByHostName("10.134.98.186");
  List<DynamicProperty> datastoresSource =
          getDynamicProarray(hMOR, "HostSystem", "datastore");
    ArrayOfManagedObjectReference dsSourceArr =
          ((ArrayOfManagedObjectReference) (datastoresSource.get(0)).getVal());
    List<ManagedObjectReference> dsTarget =
          dsSourceArr.getManagedObjectReference();
    ManagedObjectReference dsMOR = browseDSMOR(dsTarget);

// ManagedObjectReference dataStore =
// getDecendentMoRef(null, "DataStore","datastore (98186)");
ManagedObjectReference poolMOR =
getDecendentMoRef(null, "ResourcePool",pool);
VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec();
VirtualMachineRelocateSpec relocSpec = new VirtualMachineRelocateSpec();
cloneSpec.setLocation(relocSpec);
cloneSpec.setPowerOn(true);
cloneSpec.setTemplate(false);
cloneSpec.getLocation().setPool(poolMOR);
cloneSpec.getLocation().setHost(host);
cloneSpec.getLocation().setDatastore(dsMOR);

  VirtualMachineConfigSpec vmConfigSpec = new VirtualMachineConfigSpec();
  vmConfigSpec.setMemoryMB(new Long(memory));
  vmConfigSpec.setNumCPUs(Integer.valueOf(cpu));
  cloneSpec.setConfig(vmConfigSpec);

  System.out.printf("Cloning Virtual Machine [%s] to clone name [%s] %n",
        vmPathName.substring(vmPathName.lastIndexOf("/") + 1), cloneName);
  try {
     ManagedObjectReference cloneTask =
           vimPort.cloneVMTask(vmRef, vmFolderRef, cloneName, cloneSpec);

     if (getTaskResultAfterDone(cloneTask)) {
        System.out
              .printf(
                    "Successfully cloned Virtual Machine [%s] to clone name [%s] %n",
                    vmPathName.substring(vmPathName.lastIndexOf("/") + 1),
                    cloneName);
     } else {
        System.out.printf(
              "Failure Cloning Virtual Machine [%s] to clone name [%s] %n",
              vmPathName.substring(vmPathName.lastIndexOf("/") + 1),
              cloneName);
     }
  } catch (SOAPFaultException sfe) {
     printSoapFaultException(sfe);
  } catch (Exception e) {
     e.printStackTrace();
  }

求解

  1. Login to VSphere Client,

  2. go to Home

  3. go to VMs and Templates

  4. browse folder and find the virtual machine,

  5. the inventory path would be DataCenter/vm/folder structure inside DataCenter e.g. if the path in the inventory is DataCenter/folder1/folder2/machine, then it the inventory path would be DataCenter/vm/folder1/folder2/machine

注意:正确的vmpath的路机应该是 datacentername/vm/foldername/vmname 其中的vm是系统隐含的目录,必须要写上,否则一直提示找不到目录。The VMPath specified [ datacentername/foldername/vm1 ] is not found

参考:http://www.hivmr.com/db/xkap9ka8xspa11cfpksx83zspp1zf7kp