示例#1
0
 public static string getCIMSessionId (Vim25Api.ManagedObjectReference hmor1, String[] args, Cookie cookie)
 {
     AppUtil.AppUtil ecb = null;
     try
     {
         ecb = AppUtil.AppUtil.initialize("GetCIMSessioId"
                                          , args);
         ecb.connect(cookie);
         _service = ecb.getConnection()._service;
         _sic = ecb.getConnection().ServiceContent;
         ManagedObjectReference hmor = VersionUtil.convertManagedObjectReference(hmor1);
         string sessionId = _service.AcquireCimServicesTicket(hmor).sessionId;
         return sessionId;
     }
     catch (Exception e)
     {
         ecb.log.LogLine("Get GetSessionID : Failed Connect");
         throw e;
     }
     finally
     {
         ecb.log.LogLine("Ended GetSessionID");
         ecb.log.Close();
     }
 }
        /**
         * Create a Scheduled Task using the reboot method action and 
         * the weekly scheduler, for the VM found. 
         * 
         * @param taskAction action to be performed when schedule executes
         * @param scheduler the scheduler used to execute the action
         * @
         */
        private void createScheduledTask(Vim25Api.Action taskAction,
                                        TaskScheduler scheduler)
        {
            try
            {
                // Create the Scheduled Task Spec and set a unique task name
                // and description, and enable the task as soon as it is created
                String taskName = cb.get_option("taskname");
                ScheduledTaskSpec scheduleSpec = new ScheduledTaskSpec();
                scheduleSpec.name = taskName;
                scheduleSpec.description = "Reboot VM's Guest at 11.59pm every Saturday";
                scheduleSpec.enabled = true;

                // Set the RebootGuest Method Task action and 
                // the Weekly scheduler in the spec 
                scheduleSpec.action = taskAction;
                scheduleSpec.scheduler = scheduler;

                // Create the ScheduledTask for the VirtualMachine we found earlier
                ManagedObjectReference task =
                   _service.CreateScheduledTask(
                            _scheduleManager, _virtualMachine, scheduleSpec);

                // printout the MoRef id of the Scheduled Task
                Console.WriteLine("Successfully created Weekly Task: " +
                                   taskName);
            }
            catch (SoapException e)
            {
                if (e.Detail.FirstChild.LocalName.Equals("InvalidRequestFault"))
                {
                    Console.WriteLine(" InvalidRequest: vmPath may be wrong");
                }
                else if (e.Detail.FirstChild.LocalName.Equals("DuplicateNameFault"))
                {
                    Console.WriteLine("Task Name already Exists");
                }
            }

            catch (Exception e)
            {
                Console.WriteLine("Error");
                e.StackTrace.ToString();
            }
        }
示例#3
0
 public static Vim25Api.ManagedObjectReference convertManagedObjectReference(
  Vim25Api.ManagedObjectReference mor)
 {
     Vim25Api.ManagedObjectReference morr1
        = new Vim25Api.ManagedObjectReference();
     morr1.type = mor.type;
     morr1.Value = mor.Value;
     return morr1;
 }
 /**
  * Create a Scheduled Task using the poweroff method action and 
  * the onetime scheduler, for the VM found. 
  * 
  * @param taskAction action to be performed when schedule executes
  * @param scheduler the scheduler used to execute the action
  * @
  */
  
  
 private void createScheduledTask(Vim25Api.Action taskAction, 
                                 TaskScheduler scheduler) 
     {
    try {
       // Create the Scheduled Task Spec and set a unique task name
       // and description, and enable the task as soon as it is created
       String taskName = cb.get_option("taskname");
       ScheduledTaskSpec scheduleSpec = new ScheduledTaskSpec();
       scheduleSpec.name=taskName;
       scheduleSpec.description="PowerOff VM in 30 minutes";
       scheduleSpec.enabled=true;
       
       // Set the PowerOff Method Task Action and the 
       // Once scheduler in the spec 
       scheduleSpec.action=taskAction;
       scheduleSpec.scheduler=scheduler;
       
       // Create ScheduledTask for the VirtualMachine we found earlier
       if(_virtualMachine != null) {         
          ManagedObjectReference task = 
             _service.CreateScheduledTask(
                      _scheduleManager, _virtualMachine, scheduleSpec);
          // printout the MoRef id of the Scheduled Task
          Console.WriteLine("Successfully created Once Task: "
                             + taskName);
       }
       else {
          Console.WriteLine("Virtual Machine " + cb.get_option("vmname") 
                             + " not found");
          return;
       }         
    }
    catch (SoapException e)
    {
        if (e.Detail.FirstChild.LocalName.Equals("InvalidRequestFault"))
        {
            Console.WriteLine(" InvalidRequest: VMname may be wrong");
        }
        else if (e.Detail.FirstChild.LocalName.Equals("DuplicateNameFault"))
        {
            Console.WriteLine("Error :Task Name already Exists");
        }
    }
    
    catch(Exception e){
       Console.WriteLine("Error");
       e.StackTrace.ToString();
    }                    
 }