/**
         * 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();
            }
        }
        /**
         * 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();
            }
        }
        /**
         *  The main entry point for the application.
         *  @param args Arguments: <url> <user> <password> <A VM Inventory Path>
         */
        public static void Main(String[] args)
        {
            try
            {
                WeeklyRecurrenceScheduledTask schedTask = new WeeklyRecurrenceScheduledTask();
                cb = AppUtil.AppUtil.initialize("WeeklyRecurrenceScheduledTask"
                                                , WeeklyRecurrenceScheduledTask.constructOptions()
                                                , args);

                // Connect to the Service and initialize
                // any required ManagedObjectReferences
                cb.connect();
                schedTask.initialize();

                // find the VM by dns name to create a scheduled task for
                schedTask.findVirtualMachine();

                // create the power Off action to be scheduled
                Vim25Api.Action taskAction = schedTask.createTaskAction();

                // create a One time scheduler to run
                TaskScheduler taskScheduler = schedTask.createTaskScheduler();

                // Create Scheduled Task
                schedTask.createScheduledTask(taskAction,
                                              taskScheduler);

                // Disconnect from the WebService
                cb.disConnect();
                Console.WriteLine("Press any key to exit: ");
                Console.Read();
            }
            catch (Exception e)
            {
                Console.WriteLine("Caught Exception : " +
                                  " Name : " + e.Data.ToString() +
                                  " Message : " + e.Message.ToString() +
                                  " Trace : ");
                e.StackTrace.ToString();
            }
        }