private void AddTaskConfigurationSteps(EnvironmentInfo environmentInfo, string schedulerServerTasksMachineName, SchedulerAppTask schedulerAppTask, ScheduledTaskDetails taskDetails = null)
        {
            bool hasSettingsChanged = HasSettingsChanged(taskDetails, schedulerAppTask, environmentInfo);
              bool taskExists = taskDetails != null;

              EnvironmentUser environmentUser =
            environmentInfo.GetEnvironmentUserById(schedulerAppTask.UserId);

              string environmentUserPassword = null;

              if (!taskExists || hasSettingsChanged)
              {
            // collect password if not already collected
            if (!_collectedPasswordsByUserName.TryGetValue(environmentUser.UserName, out environmentUserPassword))
            {
              environmentUserPassword =
            PasswordCollectorHelper.CollectPasssword(
              _passwordCollector,
              DeploymentInfo.DeploymentId,
              environmentInfo,
              schedulerServerTasksMachineName,
              environmentUser,
              OnDiagnosticMessagePosted);

              _collectedPasswordsByUserName.Add(environmentUser.UserName, environmentUserPassword);
            }
              }

              string taskExecutablePath =
            GetTaskExecutablePath(schedulerAppTask, environmentInfo);

              if (!taskExists)
              {
            // create a step for scheduling a new app
            AddSubTask(
              new CreateSchedulerTaskDeploymentStep(
            schedulerServerTasksMachineName,
            schedulerAppTask.Name,
            taskExecutablePath,
            environmentUser.UserName,
            environmentUserPassword,
            schedulerAppTask.ScheduledHour,
            schedulerAppTask.ScheduledMinute,
            schedulerAppTask.ExecutionTimeLimitInMinutes,
            schedulerAppTask.Repetition,
            _taskScheduler));
              }
              else if (hasSettingsChanged)
              {
            // create a step for updating an existing scheduler app
            AddSubTask(
              new UpdateSchedulerTaskDeploymentStep(
            schedulerServerTasksMachineName,
            schedulerAppTask.Name,
            taskExecutablePath,
            environmentUser.UserName,
            environmentUserPassword,
            schedulerAppTask.ScheduledHour,
            schedulerAppTask.ScheduledMinute,
            schedulerAppTask.ExecutionTimeLimitInMinutes,
            schedulerAppTask.Repetition,
            _taskScheduler));
              }
        }
        private bool HasSettingsChanged(ScheduledTaskDetails taskDetails, SchedulerAppTask schedulerAppTask, EnvironmentInfo environmentInfo)
        {
            if (taskDetails == null)
              {
            return false;
              }

              string taskExecutablePath = GetTaskExecutablePath(schedulerAppTask, environmentInfo);

              return !(taskDetails.Name == schedulerAppTask.Name
              && taskDetails.ScheduledHour == schedulerAppTask.ScheduledHour
              && taskDetails.ScheduledMinute == schedulerAppTask.ScheduledMinute
              && taskDetails.ExecutionTimeLimitInMinutes == schedulerAppTask.ExecutionTimeLimitInMinutes
              && taskDetails.Repetition.Interval == schedulerAppTask.Repetition.Interval
              && taskDetails.Repetition.Duration == schedulerAppTask.Repetition.Duration
              && taskDetails.Repetition.StopAtDurationEnd == schedulerAppTask.Repetition.StopAtDurationEnd
              && taskDetails.ExeAbsolutePath == taskExecutablePath);
        }
        private static void EnsureTaskIsNotRunning(ScheduledTaskDetails taskDetails, string schedulerServerTasksMachineName)
        {
            if (taskDetails == null || !taskDetails.IsRunning)
              {
            return;
              }

              throw
            new DeploymentTaskException(
              string.Format(
            "Task: {0} on machine: {1} is already running. Deployment aborted. Last run time: {2}, next run time: {3}",
            schedulerServerTasksMachineName,
            taskDetails.Name,
            taskDetails.LastRunTime,
            taskDetails.NextRunTime));
        }