Beispiel #1
0
        /// <summary>
        /// Creates a specialized class from a defined interface.
        /// </summary>
        /// <param name="iTask">Version 1.0 interface.</param>
        /// <returns>Specialized action class</returns>
        internal static Action CreateAction(V1Interop.ITask iTask)
        {
            ExecAction tempAction = new ExecAction(iTask);
            Action     a          = ConvertFromPowerShellAction(tempAction);

            return(a ?? tempAction);
        }
 internal void UnconvertUnsupportedActions()
 {
     if (TaskService.LibraryVersion.Minor > 3)
     {
         for (int i = 0; i < this.Count; i++)
         {
             ExecAction action = this[i] as ExecAction;
             if (action != null && action.Arguments != null && action.Arguments.Contains(ExecAction.ScriptIdentifer))
             {
                 var match = System.Text.RegularExpressions.Regex.Match(action.Arguments, @"<# " + ExecAction.ScriptIdentifer + ":(?<type>\\w+) #> (?<cmd>.+)}\"$");
                 if (match.Success)
                 {
                     Action newAction = null;
                     if (match.Groups["type"].Value == "SendEmail")
                     {
                         newAction = EmailAction.FromPowerShellCommand(match.Groups["cmd"].Value);
                     }
                     else if (match.Groups["type"].Value == "ShowMessage")
                     {
                         newAction = ShowMessageAction.FromPowerShellCommand(match.Groups["cmd"].Value);
                     }
                     if (newAction != null)
                     {
                         this[i] = newAction;
                     }
                 }
             }
         }
     }
 }
        public void Dev2ActionCollection_Enumerate()
        {
            var collection = new Dev2ActionCollection(_taskServiceConvertorFactory.Object, _nativeInstance);
            var nativeAction = new ExecAction("a", "b", "c");
            var actionToAdd = new Dev2ExecAction(_taskServiceConvertorFactory.Object, nativeAction);
            _taskServiceConvertorFactory.Setup(a => a.CreateAction(nativeAction)).Returns(actionToAdd);
            collection.Add(actionToAdd);
            var nativeAction1 = new ExecAction("1", "2", "3");
            var nativeAction2 = new ExecAction("4", "6", "4");
            var actionToAdd1 = new Dev2ExecAction(_taskServiceConvertorFactory.Object, nativeAction1);
            var actionToAdd2 = new Dev2ExecAction(_taskServiceConvertorFactory.Object, nativeAction2);
            _taskServiceConvertorFactory.Setup(a => a.CreateAction(It.IsAny<ExecAction>())).Returns(actionToAdd);

            collection.Add( actionToAdd1);
            collection.Add( actionToAdd2);
            var e =collection.GetEnumerator();
            Assert.IsTrue(e.MoveNext());
            Assert.AreEqual(((ExecAction)e.Current.Instance).Path,"a");

            _taskServiceConvertorFactory.Setup(a => a.CreateAction(It.IsAny<ExecAction>())).Returns(actionToAdd1);
            Assert.IsTrue(e.MoveNext());
            Assert.AreEqual(((ExecAction)e.Current.Instance).Path, "1");

            _taskServiceConvertorFactory.Setup(a => a.CreateAction(It.IsAny<ExecAction>())).Returns(actionToAdd2);
            Assert.IsTrue(e.MoveNext());
            Assert.AreEqual(((ExecAction)e.Current.Instance).Path, "4");

            Assert.IsFalse(e.MoveNext());
        }
        public void Init()
        {
            _taskService = new TaskService();

            var newTask = _taskService.NewTask();
            var action = new ExecAction("bob.exe");
            newTask.Actions.Add(action);
            newTask.Triggers.Add(new DailyTrigger());
            _taskService.RootFolder.RegisterTaskDefinition("UnitTestTask",newTask);
            _factory = new Mock<ITaskServiceConvertorFactory>();
        }
Beispiel #5
0
 public void TaskShedulerWrapper_Dev2Action_Construct()
 {
     using ( Microsoft.Win32.TaskScheduler.Action act = new ExecAction("bob","dave","jane"))
     {
         Dev2Action wrapped = new Dev2Action(act);
         wrapped.Id = Guid.NewGuid().ToString();
         Assert.AreEqual(act.ActionType, wrapped.ActionType);
         Assert.AreEqual(act.Id, wrapped.Id);
         Assert.AreEqual(act, wrapped.Instance);
     }
 }
 internal void ConvertUnsupportedActions()
 {
     if (TaskService.LibraryVersion.Minor > 3 && SupportV2Conversion)
     {
         for (int i = 0; i < Count; i++)
         {
             Action action   = this[i];
             var    bindable = action as IBindAsExecAction;
             if (bindable != null && !(action is ComHandlerAction))
             {
                 this[i] = ExecAction.ConvertToPowerShellAction(action);
             }
         }
     }
 }
Beispiel #7
0
        internal static Action ConvertFromPowerShellAction(ExecAction execAction)
        {
            var psi = execAction.ParsePowerShellItems();

            if (psi != null && psi.Length == 2)
            {
                var a = ActionFromScript(psi[0], psi[1]);
                if (a != null)
                {
                    a.v1Task  = execAction.v1Task;
                    a.iAction = execAction.iAction;
                    return(a);
                }
            }
            return(null);
        }
 internal void ConvertUnsupportedActions()
 {
     if (TaskService.LibraryVersion.Minor > 3)
     {
         for (int i = 0; i < this.Count; i++)
         {
             Action action   = this[i];
             var    bindable = action as IBindAsExecAction;
             if (bindable != null)
             {
                 string cmd = bindable.GetPowerShellCommand();
                 this[i] = ExecAction.AsPowerShellCmd(action.ActionType.ToString(), cmd);
             }
         }
     }
 }
 private void UnconvertUnsupportedActions()
 {
     if (TaskService.LibraryVersion.Minor > 3)
     {
         for (int i = 0; i < Count; i++)
         {
             ExecAction action = this[i] as ExecAction;
             if (action != null)
             {
                 Action newAction = Action.ConvertFromPowerShellAction(action);
                 if (newAction != null)
                 {
                     this[i] = newAction;
                 }
             }
         }
     }
 }
        private void SaveV1Actions()
        {
            if (v1Task == null)
            {
                throw new ArgumentNullException(nameof(v1Task));
            }
            if (v1Actions.Count == 0)
            {
                v1Task.SetApplicationName(string.Empty);
                v1Task.SetParameters(string.Empty);
                v1Task.SetWorkingDirectory(string.Empty);
                v1Task.SetDataItem("ActionId", null);
                v1Task.SetDataItem("ActionType", "EMPTY");
            }
            else if (v1Actions.Count == 1)
            {
                if (!SupportV1Conversion && v1Actions[0].ActionType != TaskActionType.Execute)
                {
                    throw new NotV1SupportedException($"Only a single {nameof(ExecAction)} is supported unless the {nameof(PowerShellConversion)} property includes the {nameof(PowerShellActionPlatformOption.Version1)} value.");
                }
                v1Task.SetDataItem("ActionType", null);
                v1Actions[0].Bind(v1Task);
            }
            else
            {
                if (!SupportV1Conversion)
                {
                    throw new NotV1SupportedException($"Only a single {nameof(ExecAction)} is supported unless the {nameof(PowerShellConversion)} property includes the {nameof(PowerShellActionPlatformOption.Version1)} value.");
                }
                // Build list of internal PowerShell scripts
                var sb = new System.Text.StringBuilder();
                foreach (var item in v1Actions)
                {
                    sb.Append($"<# {item.Id ?? "NO_ID"}:{item.ActionType} #> {item.GetPowerShellCommand()} ");
                }

                // Build and save PS ExecAction
                var ea = ExecAction.CreatePowerShellAction("MULTIPLE", sb.ToString());
                ea.Bind(v1Task);
                v1Task.SetDataItem("ActionId", null);
                v1Task.SetDataItem("ActionType", "MULTIPLE");
            }
        }
Beispiel #11
0
        public static void TrySetLaunchAtStartup(bool enabled, string username = null, string userpassword = null)
        {
            string name = Application.ProductName;

            using (MW32TS.TaskService service = new MW32TS.TaskService())
            {
                TryDeleteTasksByName(name);

                if (!enabled)
                {
                    return;
                }

                MW32TS.LogonTrigger trigger = new MW32TS.LogonTrigger();


                // MW32TS.BootTrigger trigger = new MW32TS.BootTrigger();
                // trigger.Delay = new TimeSpan(0, 0, 5);
                //trigger.Enabled = true;

                MW32TS.ExecAction action = new MW32TS.ExecAction(Application.ExecutablePath, null, null);
                action.WorkingDirectory = Application.StartupPath;

                MW32TS.TaskDefinition definition = service.NewTask();
                definition.RegistrationInfo.Description = "Launches App At Startup";
                definition.Triggers.Add(trigger);
                definition.Actions.Add(action);

                definition.Principal.RunLevel = MW32TS.TaskRunLevel.Highest;
                //definition.Principal.
                if (!username.IsNullOrEmpty())
                {
                    service.UserName = username;
                }

                if (!userpassword.IsNullOrEmpty())
                {
                    service.UserPassword = userpassword;
                }

                service.RootFolder.RegisterTaskDefinition(name, definition);
            }
        }
        private List <Action> GetV1Actions()
        {
            List <Action> ret = new List <Action>();

            if (v1Task != null && v1Task.GetDataItem("ActionType") != "EMPTY")
            {
                var exec  = new ExecAction(v1Task);
                var items = exec.ParsePowerShellItems();
                if (items != null)
                {
                    if (items.Length == 2 && items[0] == "MULTIPLE")
                    {
                        PowerShellConversion |= PowerShellActionPlatformOption.Version1;
                        var mc = System.Text.RegularExpressions.Regex.Matches(items[1], @"<# (?<id>\w+):(?<t>\w+) #>\s*(?<c>[^<#]*)\s*");
                        foreach (System.Text.RegularExpressions.Match ms in mc)
                        {
                            var a = Action.ActionFromScript(ms.Groups["t"].Value, ms.Groups["c"].Value);
                            if (a != null)
                            {
                                if (ms.Groups["id"].Value != "NO_ID")
                                {
                                    a.Id = ms.Groups["id"].Value;
                                }
                                ret.Add(a);
                            }
                        }
                    }
                    else
                    {
                        ret.Add(ExecAction.ConvertFromPowerShellAction(exec));
                    }
                }
                else if (!string.IsNullOrEmpty(exec.Path))
                {
                    ret.Add(exec);
                }
            }
            return(ret);
        }
Beispiel #13
0
        internal virtual void Bind(V1Interop.ITask iTask)
        {
            if (Id != null)
            {
                iTask.SetDataItem("ActionId", Id);
            }
            IBindAsExecAction bindable = this as IBindAsExecAction;

            if (bindable != null)
            {
                iTask.SetDataItem("ActionType", InternalActionType.ToString());
            }
            object o = null;

            unboundValues.TryGetValue("Path", out o);
            iTask.SetApplicationName(bindable != null ? ExecAction.PowerShellPath : o?.ToString() ?? string.Empty);
            o = null;
            unboundValues.TryGetValue("Arguments", out o);
            iTask.SetParameters(bindable != null ? ExecAction.BuildPowerShellCmd(ActionType.ToString(), GetPowerShellCommand()) : o?.ToString() ?? string.Empty);
            o = null;
            unboundValues.TryGetValue("WorkingDirectory", out o);
            iTask.SetWorkingDirectory(o?.ToString() ?? string.Empty);
        }
 private Dev2ActionCollection CreateCollection()
 {
     var collection = new Dev2ActionCollection(_taskServiceConvertorFactory.Object, _nativeInstance);
     var nativeAction = new ExecAction("a", "b", "c");
     var actionToAdd = new Dev2ExecAction(_taskServiceConvertorFactory.Object, nativeAction);
     _taskServiceConvertorFactory.Setup(a => a.CreateAction(nativeAction)).Returns(actionToAdd);
     collection.Add(actionToAdd);
     return collection;
 }
 public Dev2ExecAction(ITaskServiceConvertorFactory taskServiceConvertorFactory, ExecAction nativeTnstance)
     : base(nativeTnstance)
 {
 }
Beispiel #16
0
        public void ScheduleNewTask(string machineName, ScheduledTaskSpecification scheduledTaskSpecification, string userName, string password)
        {
            Guard.NotNullNorEmpty(machineName, "machineName");
              Guard.NotNull(scheduledTaskSpecification, "scheduledTaskSpecification");
              Guard.NotNullNorEmpty(userName, "userName");
              Guard.NotNullNorEmpty(password, "password");

              using (var taskService = CreateTaskService(machineName))
              {
            Task task = taskService.FindTask(scheduledTaskSpecification.Name, false);

            if (task != null)
            {
              task.Dispose();

              throw new InvalidOperationException(string.Format("Couldn't schedule new task because a task with the same name ('{0}') has already been scheduled.", scheduledTaskSpecification.Name));
            }

            Action taskAction =
              new ExecAction(scheduledTaskSpecification.ExeAbsolutePath)
            {
              WorkingDirectory = Path.GetDirectoryName(scheduledTaskSpecification.ExeAbsolutePath),
            };

            DailyTrigger taskTrigger = CreateTaskTrigger(scheduledTaskSpecification);
            TaskDefinition taskDefinition = null;
            Task registeredTask = null;

            try
            {
              taskDefinition = taskService.NewTask();

              taskDefinition.Settings.AllowDemandStart = true;
              taskDefinition.Settings.AllowHardTerminate = true;
              taskDefinition.Settings.DisallowStartIfOnBatteries = false;
              taskDefinition.Settings.DisallowStartOnRemoteAppSession = false;
              taskDefinition.Settings.RunOnlyIfIdle = false;
              taskDefinition.Settings.RunOnlyIfNetworkAvailable = false;
              taskDefinition.Settings.StartWhenAvailable = true;
              taskDefinition.Settings.StopIfGoingOnBatteries = false;

              taskDefinition.RegistrationInfo.Source = _TaskRegistrationInfoSource;

              taskDefinition.Actions.Add(taskAction);
              taskDefinition.Triggers.Add(taskTrigger);

              registeredTask =
            taskService.RootFolder.RegisterTaskDefinition(
              scheduledTaskSpecification.Name,
              taskDefinition,
              TaskCreation.Create,
              userName,
              password,
              TaskLogonType.Password);
            }
            finally
            {
              if (taskDefinition != null)
              {
            taskDefinition.Dispose();
              }

              if (registeredTask != null)
              {
            registeredTask.Dispose();
              }
            }
              }
        }