示例#1
0
      private void cloneVM() {    
           _service = cb.getConnection()._service;
           _sic = cb.getConnection()._sic;
    String cloneName = cb.get_option("CloneName");
    String vmPath = cb.get_option("vmPath");
    String datacenterName= cb.get_option("DatacenterName");
    
   
    // Find the Datacenter reference by using findByInventoryPath().
    ManagedObjectReference datacenterRef
       = _service.FindByInventoryPath(_sic.searchIndex, datacenterName);
    if (datacenterRef == null) {
       Console.WriteLine("The specified datacenter is not found");
       return;
    }
    // Find the virtual machine folder for this datacenter.
    ManagedObjectReference vmFolderRef
       = (ManagedObjectReference)cb.getServiceUtil().GetMoRefProp(datacenterRef, "vmFolder");
    if (vmFolderRef == null) {
       Console.WriteLine("The virtual machine is not found");
       return;
    }
    ManagedObjectReference vmRef
       = _service.FindByInventoryPath(_sic.searchIndex, vmPath);
    if (vmRef == null) {
       Console.WriteLine("The virtual machine is not found");
       return;
    }
    VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec();
    VirtualMachineRelocateSpec relocSpec = new VirtualMachineRelocateSpec();
    cloneSpec.location=relocSpec;
    cloneSpec.powerOn=false;
    cloneSpec.template=false;
    
    String clonedName = cloneName;
    Console.WriteLine("Launching clone task to create a clone: " 
                       + clonedName);
    try {
       ManagedObjectReference cloneTask 
          = _service.CloneVM_Task(vmRef, vmFolderRef, clonedName, cloneSpec);
       String status = cb.getServiceUtil().WaitForTask(cloneTask);
       if(status.Equals("failure")) {
          Console.WriteLine("Failure -: Virtual Machine cannot be cloned");
       }
       if (status.Equals("sucess"))
       {
          Console.WriteLine("Virtual Machine Cloned  successfully.");
       }
       else{
           Console.WriteLine("Virtual Machine Cloned cannot be cloned");
    }
    }
    catch(Exception e) {
      
    }   
 }
示例#2
0
        public Boolean findVirtualMachine()
        {
            String vmPath = cb.get_option("vmpath");

            // Console.Write(vmPath);
            _virtualMachine = _service.FindByInventoryPath(_searchIndex, vmPath);
            if (_virtualMachine != null)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
示例#3
0
        public void deploy(VM guest)
        {
            cb = AppUtil.AppUtil.initialize("VMDeploy", this.connectString);
            cb.connect();
            /************Start Deploy Code*****************/
            _service = cb.getConnection()._service;
            _sic = cb.getConnection()._sic;

            // ManagedObjectReferences
            ManagedObjectReference datacenterRef;
            ManagedObjectReference vmFolderRef;
            ManagedObjectReference vmRef;
            ManagedObjectReference hfmor; // hostFolder reference
            ArrayList crmors; // ArrayList of ComputeResource references
            ManagedObjectReference hostmor;
            ManagedObjectReference crmor = null; // ComputeResource reference
            ManagedObjectReference resourcePool;

            // Find the Datacenter reference by using findByInventoryPath().
            datacenterRef = _service.FindByInventoryPath(_sic.searchIndex, this.dataCenter);

            if (datacenterRef == null)
            {
                Console.WriteLine("The specified datacenter is not found");
                return;
            }

            // Find the virtual machine folder for this datacenter.
            vmFolderRef = (ManagedObjectReference)cb.getServiceUtil().GetMoRefProp(datacenterRef, "vmFolder");
            if (vmFolderRef == null)
            {
                Console.WriteLine("The virtual machine is not found");
                return;
            }

            vmRef = _service.FindByInventoryPath(_sic.searchIndex, guest.getVmPath());
            if (vmRef == null)
            {
                Console.WriteLine("The virtual machine is not found");
                return;
            }

            // Code for obtaining managed object reference to resource root

            hfmor = cb.getServiceUtil().GetMoRefProp(datacenterRef, "hostFolder");
            crmors = cb.getServiceUtil().GetDecendentMoRefs(hfmor, "ComputeResource", null);

            if (this.hostName != null)
            {
                hostmor = cb.getServiceUtil().GetDecendentMoRef(hfmor, "HostSystem", this.hostName);
                if (hostmor == null)
                {
                    Console.WriteLine("Host " + this.hostName + " not found");
                    return;
                }
            }
            else
            {
                hostmor = cb.getServiceUtil().GetFirstDecendentMoRef(datacenterRef, "HostSystem");
            }

            hostName = (String)cb.getServiceUtil().GetDynamicProperty(hostmor, "name");
            for (int i = 0; i < crmors.Count; i++)
            {

                ManagedObjectReference[] hrmors
                   = (ManagedObjectReference[])cb.getServiceUtil().GetDynamicProperty((ManagedObjectReference)crmors[i], "host");
                if (hrmors != null && hrmors.Length > 0)
                {
                    for (int j = 0; j < hrmors.Length; j++)
                    {
                        String hname = (String)cb.getServiceUtil().GetDynamicProperty(hrmors[j], "name");
                        if (hname.Equals(this.hostName))
                        {
                            crmor = (ManagedObjectReference)crmors[i];
                            i = crmors.Count + 1;
                            j = hrmors.Length + 1;
                        }

                    }
                }
            }

            if (crmor == null)
            {
                Console.WriteLine("No Compute Resource Found On Specified Host");
                return;
            }
            resourcePool = cb.getServiceUtil().GetMoRefProp(crmor, "resourcePool");

            /***********************************/
            /*Setup cloning sysprep preferences*/
            /***********************************/

            VirtualMachineCloneSpec cloneSpec = new VirtualMachineCloneSpec();
            VirtualMachineRelocateSpec relocSpec = new VirtualMachineRelocateSpec();

            // Set resource pool for relocspec(compulsory since deploying template)
            relocSpec.pool = resourcePool;

            cloneSpec.location = relocSpec;
            cloneSpec.powerOn = true; //Specifies whether or not the new VirtualMachine should be powered on after creation. As part of a customization, this flag is normally set to true, since the first power-on operation completes the customization process. This flag is ignored if a template is being created.
            cloneSpec.template = false; //Specifies whether or not the new virtual machine should be marked as a template.

            // Customization
            CustomizationSpec custSpec = new CustomizationSpec();

            // Make NIC settings
            CustomizationAdapterMapping[] custAdapter = new CustomizationAdapterMapping[1];
            custAdapter[0] = new CustomizationAdapterMapping();
            CustomizationIPSettings custIPSettings = new CustomizationIPSettings();
            CustomizationDhcpIpGenerator custDhcp = new CustomizationDhcpIpGenerator();
            custIPSettings.ip = custDhcp;
            custAdapter[0].adapter = custIPSettings;
            // Set NIC settings
            custSpec.nicSettingMap = custAdapter;

            // Make DNS entry
            CustomizationGlobalIPSettings custIP = new CustomizationGlobalIPSettings();
            custIP.dnsServerList = guest.getDnsList(); ;
            // Set DNS entry
            custSpec.globalIPSettings = custIP;

            // Make Sysprep entries
            CustomizationSysprep custPrep = new CustomizationSysprep(); //An object representation of a Windows sysprep.inf answer file. The sysprep type encloses all the individual keys listed in a sysprep.inf file

            // Make guiRunOnce entries(to change autologon settings to login to domain)

            //CustomizationGuiRunOnce custGuiRunOnce = new CustomizationGuiRunOnce();

            //string deleteKey = "reg delete \"HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\" /v \"DefaultDomainName\" /f";
            //string addKey = "reg add \"HKLM\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Winlogon\" /v \"DefaultDomainName\" /t REG_SZ /d " + this.joinDomain;
            //string shutdownKey = "shutdown -r -t 00 -c \"Rebooting computer\"";

            //custGuiRunOnce.commandList = new string[] { deleteKey, addKey, shutdownKey };

            // Set guiRunOnce
            //custPrep.guiRunOnce = custGuiRunOnce;

            // Make guiUnattended settings
            CustomizationGuiUnattended custGui = new CustomizationGuiUnattended(); //The GuiUnattended type maps to the GuiUnattended key in the sysprep.inf answer file
            //The GuiUnattended type maps to the GuiUnattended key in the sysprep.inf answer file

            if (Int32.Parse(guest.getAutoLogonCount()) == 0)
            {
                custGui.autoLogon = false;
            }
            else
            {
                custGui.autoLogon = true;
                custGui.autoLogonCount = Int16.Parse(guest.getAutoLogonCount()); //If the AutoLogon flag is set, then the AutoLogonCount property specifies the number of times the machine should automatically log on as Administrator
            }

            CustomizationPassword custWorkPass = new CustomizationPassword();

            if (guest.getWorkGroupPassword() != null)
            {
                custWorkPass.plainText = true; //Flag to specify whether or not the password is in plain text, rather than encrypted.
                custWorkPass.value = guest.getWorkGroupPassword();
                custGui.password = custWorkPass;
            }

            custGui.timeZone = 190; //IST The time zone for the new virtual machine. Numbers correspond to time zones listed in sysprep documentation at  in Microsoft Technet. Taken from unattend.txt

            // Set guiUnattend settings
            custPrep.guiUnattended = custGui;

            // Make identification settings
            CustomizationIdentification custId = new CustomizationIdentification();
            custId.domainAdmin = guest.getDomainAdmin();
            CustomizationPassword custPass = new CustomizationPassword();
            custPass.plainText = true; //Flag to specify whether or not the password is in plain text, rather than encrypted.
            custPass.value = guest.getDomainPassword();
            custId.domainAdminPassword = custPass;
            custId.joinDomain = guest.getJoinDomain();
            // Set identification settings
            custPrep.identification = custId;

            // Make userData settings
            CustomizationUserData custUserData = new CustomizationUserData();
            CustomizationFixedName custName = new CustomizationFixedName();
            custName.name = guest.getName();
            custUserData.computerName = custName;
            custUserData.fullName = "ePO";
            custUserData.orgName = "McAfee";

            if (guest.getProductId() != null)
            {
                custUserData.productId = guest.getProductId();
            }

            // Set userData settings
            custPrep.userData = custUserData;

            // Set sysprep
            custSpec.identity = custPrep;

            // clonespec customization
            cloneSpec.customization = custSpec;

            // clone power on
            cloneSpec.powerOn = true;

            String clonedName = guest.getName();
            Console.WriteLine("Launching clone task to create a clone: " + clonedName);

            try
            {
                ManagedObjectReference cloneTask
                   = _service.CloneVM_Task(vmRef, vmFolderRef, clonedName, cloneSpec);
                String status = cb.getServiceUtil().WaitForTask(cloneTask);
                if (status.Equals("failure"))
                {
                    Console.WriteLine("Failure -: Virtual Machine cannot be cloned");
                }
                if (status.Equals("sucess"))
                {
                    Console.WriteLine("Virtual Machine Cloned  successfully.");
                }
                else
                {
                    Console.WriteLine("Virtual Machine Cloned cannot be cloned");
                }
            }
            catch (Exception e)
            {

            }
            /************End Deploy Code*******************/
            cb.disConnect();
        }
        /**
         * Use the SearchIndex to find a VirtualMachine by Inventory Path
         *
         * @
         */
        private void findVirtualMachine()
        {
            String vmPath = cb.get_option("vmpath");

            _virtualMachine = _service.FindByInventoryPath(_searchIndex, vmPath);
        }
示例#5
0
        /** Excercise the SearchIndex API
         *
         * @param args Usage is:
         *  <url> <user> <password> <Datacenter Name> <A VM DNS Name>
         *  <A Host DNS Name> <A VM Inventory Path>
         */
        public static void Main(String[] args)
        {
            try {
                SearchIndex app = new SearchIndex();
                cb = AppUtil.AppUtil.initialize("SearchIndex"
                                                , SearchIndex.constructOptions()
                                                , args);
                cb.connect();
                String dcName      = cb.get_option("dcName");
                String vmDnsName   = cb.get_option("vmDnsName");
                String hostDnsName = cb.get_option("hostDnsName");
                String vmPath      = cb.get_option("vmPath");
                String vmIP        = cb.get_option("vmIP");

                content = cb.getConnection()._sic;
                service = cb.getConnection()._service;
                // Find the Datacenter by using findChild()
                ManagedObjectReference dcMoRef =
                    cb.getServiceUtil().GetDecendentMoRef(null, "Datacenter", dcName);
                if (dcMoRef != null)
                {
                    Console.WriteLine("Found Datacenter with name: "
                                      + dcName + ", MoRef: " +
                                      dcMoRef.Value);
                }
                else
                {
                    Console.WriteLine("Datacenter not Found with name: " + dcName);
                }
                if (vmDnsName != null)
                {
                    ManagedObjectReference vmMoRef =
                        service.FindByDnsName(content.searchIndex,
                                              dcMoRef,
                                              vmDnsName,
                                              true);
                    if (vmMoRef != null)
                    {
                        Console.WriteLine("Found VirtualMachine with DNS name: " +
                                          vmDnsName + ", MoRef: " + vmMoRef.Value);
                    }
                    else
                    {
                        Console.WriteLine("VirtualMachine not Found with DNS name: "
                                          + vmDnsName);
                    }
                }
                if (vmPath != null)
                {
                    ManagedObjectReference vmMoRef = service.FindByInventoryPath(
                        content.searchIndex, vmPath);
                    if (vmMoRef != null)
                    {
                        Console.WriteLine("Found VirtualMachine with Path: " +
                                          vmPath + ", MoRef: " + vmMoRef.Value);
                    }
                    else
                    {
                        Console.WriteLine("VirtualMachine not found with vmPath " +
                                          "address: " + vmPath);
                    }
                }
                if (vmIP != null)
                {
                    ManagedObjectReference vmMoRef =
                        service.FindByIp(content.searchIndex,
                                         dcMoRef,
                                         vmIP,
                                         true);
                    if (vmMoRef != null)
                    {
                        Console.WriteLine("Found VirtualMachine with IP " +
                                          "address " + vmIP + ", MoRef: " + vmMoRef.Value);
                    }
                    else
                    {
                        Console.WriteLine("VirtualMachine not found with IP " +
                                          "address: " + vmIP);
                    }
                }
                if (hostDnsName != null)
                {
                    ManagedObjectReference hostMoRef =
                        service.FindByDnsName(content.searchIndex,
                                              null,
                                              hostDnsName,
                                              false);
                    if (hostMoRef != null)
                    {
                        Console.WriteLine("Found HostSystem with DNS name " +
                                          hostDnsName + ", MoRef: " + hostMoRef.Value);
                    }
                    else
                    {
                        Console.WriteLine("HostSystem not Found with DNS name:" +
                                          hostDnsName);
                    }
                }
                cb.disConnect();
                Console.WriteLine("Press enter to exit: ");
                Console.Read();
            }
            catch (SoapException e) {
            }
        }
示例#6
0
    /** Excercise the SearchIndex API
     * 
     * @param args Usage is:
     *  <url> <user> <password> <Datacenter Name> <A VM DNS Name>
     *  <A Host DNS Name> <A VM Inventory Path> 
     */
    public static void Main(String[] args) {
        
       try {
          SearchIndex app = new SearchIndex();
            cb = AppUtil.AppUtil.initialize("SearchIndex"
                                    ,SearchIndex.constructOptions()
                                    ,args);
            cb.connect();
            String dcName = cb.get_option("dcName");
            String vmDnsName = cb.get_option("vmDnsName");
            String hostDnsName =cb.get_option("hostDnsName");
            String vmPath = cb.get_option("vmPath");
            String vmIP = cb.get_option("vmIP");
            
            content = cb.getConnection()._sic;
            service = cb.getConnection()._service;
            // Find the Datacenter by using findChild()
            ManagedObjectReference dcMoRef = 
            cb.getServiceUtil().GetDecendentMoRef(null, "Datacenter", dcName);
            if (dcMoRef !=null){
                    Console.WriteLine("Found Datacenter with name: "
                                       +dcName+", MoRef: " + 
                    dcMoRef.Value);
            }else{
                   Console.WriteLine("Datacenter not Found with name: "+dcName);
            }
            if (vmDnsName != null){
                ManagedObjectReference vmMoRef = 
                service.FindByDnsName(content.searchIndex,
                                      dcMoRef,
                                      vmDnsName,
                                      true);
               if (vmMoRef !=null){
                 Console.WriteLine("Found VirtualMachine with DNS name: "+
                      vmDnsName+", MoRef: " + vmMoRef.Value);
               }
               else{
                 Console.WriteLine("VirtualMachine not Found with DNS name: "
                                  + vmDnsName);
               }
            }
            if (vmPath != null){
              ManagedObjectReference   vmMoRef = service.FindByInventoryPath(
                    content.searchIndex, vmPath);
               if (vmMoRef !=null) {
                  Console.WriteLine("Found VirtualMachine with Path: "+
                      vmPath+", MoRef: " + vmMoRef.Value);
            
               }
               else{
                      Console.WriteLine("VirtualMachine not found with vmPath "+
                      "address: "+ vmPath);
                }
             }
             if (vmIP != null){
                ManagedObjectReference vmMoRef =
                service.FindByIp(content.searchIndex,
                                 dcMoRef,
                                 vmIP,
                                 true);
              if (vmMoRef !=null){
                    Console.WriteLine("Found VirtualMachine with IP "+
                    "address "+ vmIP + ", MoRef: " + vmMoRef.Value);
              }else{
                   Console.WriteLine("VirtualMachine not found with IP "+
                   "address: "+vmIP);
               }
            }
            if (hostDnsName != null) {
               ManagedObjectReference hostMoRef =
                service.FindByDnsName(content.searchIndex,
                        null,
                        hostDnsName,
                        false);
               if (hostMoRef !=null) {
                  Console.WriteLine("Found HostSystem with DNS name "+
                  hostDnsName+", MoRef: " + hostMoRef.Value);
               }
               else{
                  Console.WriteLine("HostSystem not Found with DNS name:"+
                  hostDnsName);
               }
            }
          cb.disConnect();
          Console.WriteLine("Press enter to exit: ");
          Console.Read();
          
       } 
       catch(SoapException e) {

       }
    }
示例#7
0
        private void cloneVM()
        {
            _service = cb.getConnection()._service;
            _sic     = cb.getConnection()._sic;
            String cloneName      = cb.get_option("CloneName");
            String vmPath         = cb.get_option("vmPath");
            String datacenterName = cb.get_option("DatacenterName");


            // Find the Datacenter reference by using findByInventoryPath().
            ManagedObjectReference datacenterRef
                = _service.FindByInventoryPath(_sic.searchIndex, datacenterName);

            if (datacenterRef == null)
            {
                Console.WriteLine("The specified datacenter is not found");
                return;
            }
            // Find the virtual machine folder for this datacenter.
            ManagedObjectReference vmFolderRef
                = (ManagedObjectReference)cb.getServiceUtil().GetMoRefProp(datacenterRef, "vmFolder");

            if (vmFolderRef == null)
            {
                Console.WriteLine("The virtual machine is not found");
                return;
            }
            ManagedObjectReference vmRef
                = _service.FindByInventoryPath(_sic.searchIndex, vmPath);

            if (vmRef == null)
            {
                Console.WriteLine("The virtual machine is not found");
                return;
            }
            VirtualMachineCloneSpec    cloneSpec = new VirtualMachineCloneSpec();
            VirtualMachineRelocateSpec relocSpec = new VirtualMachineRelocateSpec();

            cloneSpec.location = relocSpec;
            cloneSpec.powerOn  = false;
            cloneSpec.template = false;

            String clonedName = cloneName;

            Console.WriteLine("Launching clone task to create a clone: "
                              + clonedName);
            try {
                ManagedObjectReference cloneTask
                    = _service.CloneVM_Task(vmRef, vmFolderRef, clonedName, cloneSpec);
                String status = cb.getServiceUtil().WaitForTask(cloneTask);
                if (status.Equals("failure"))
                {
                    Console.WriteLine("Failure -: Virtual Machine cannot be cloned");
                }
                if (status.Equals("sucess"))
                {
                    Console.WriteLine("Virtual Machine Cloned  successfully.");
                }
                else
                {
                    Console.WriteLine("Virtual Machine Cloned cannot be cloned");
                }
            }
            catch (Exception e) {
            }
        }