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; } } } } } }
internal static Action FromPowerShellCommand(string p) { var match = System.Text.RegularExpressions.Regex.Match(p, @"^Send-MailMessage -From '(?<from>(?:[^']|'')*)' -Subject '(?<subject>(?:[^']|'')*)' -SmtpServer '(?<server>(?:[^']|'')*)'(?: -Encoding UTF8)?(?: -To (?<to>'(?:(?:[^']|'')*)'(?:, '(?:(?:[^']|'')*)')*))?(?: -Cc (?<cc>'(?:(?:[^']|'')*)'(?:, '(?:(?:[^']|'')*)')*))?(?: -Bcc (?<bcc>'(?:(?:[^']|'')*)'(?:, '(?:(?:[^']|'')*)')*))?(?:(?: -BodyAsHtml)? -Body '(?<body>(?:[^']|'')*)')?(?: -Attachments (?<att>'(?:(?:[^']|'')*)'(?:, '(?:(?:[^']|'')*)')*))?\s*$"); if (match.Success) { EmailAction action = new EmailAction(UnPrep(FromUTF8(match.Groups["subject"].Value)), UnPrep(match.Groups["from"].Value), FromPS(match.Groups["to"]), UnPrep(FromUTF8(match.Groups["body"].Value)), UnPrep(match.Groups["server"].Value)) { Cc = FromPS(match.Groups["cc"]), Bcc = FromPS(match.Groups["bcc"]) }; if (match.Groups["att"].Success) { action.Attachments = Array.ConvertAll <string, object>(FromPS(match.Groups["att"].Value), s => s); } return(action); } return(null); }
internal static void ShortTest(TaskService ts, System.IO.TextWriter output, params string[] arg) { // Get the service on the local machine try { /*string sub = "<QueryList><Query Id=\"0\" Path=\"Microsoft-Windows-TaskScheduler/Operational\">" + "<Select Path=\"Microsoft-Windows-TaskScheduler/Operational\">" + "*[System[Provider[@Name='Microsoft-Windows-TaskScheduler'] and (Computer='dahall1') and (Level=0 or Level=4) and (Task=100 or Task=101) and (EventID=129) and Security[@UserID='AMERICAS\\dahall'] and TimeCreated[timediff(@SystemTime) <= 86400000]]]" + "*[EventData[Data[@Name='TaskName']='\\Maint' and Data[@Name='EventCode']='0']]" + "</Select>" + "</Query></QueryList>"; using (var ed = new EventActionFilterEditor() { Subscription = sub }) { ed.ShowDialog(); } return;*/ /*Action<string> d = delegate(string s) { var ar = s.Split('|'); foreach (System.Text.RegularExpressions.Match m in System.Text.RegularExpressions.Regex.Matches(ar[2], @"\(A;(?<Flag>\w*);(?<Right>\w*);(?<Guid>\w*);(?<OIGuid>\w*);(?<Acct>[\w\-\d]*)(?:;[^\)]*)?\)")) output.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}", ar[0], ar[1], m.Groups["Flag"], m.Groups["Right"], m.Groups["Guid"], m.Groups["OIGuid"], m.Groups["Acct"]); }; FolderTaskAction(ts.RootFolder, delegate(TaskFolder f) { d("F|" + f.Name + "|" + f.GetSecurityDescriptorSddlForm()); }, delegate(Task s) { d("T|" + s.Name + "|" + s.GetSecurityDescriptorSddlForm()); }); return;*/ //FolderTaskAction(ts.RootFolder, null, delegate(Task tsk) { if (tsk.Definition.Triggers.ContainsType(typeof(CustomTrigger))) output.WriteLine(tsk.Path); }); // Create a new task definition and assign properties const string taskName = "Test"; TaskDefinition td = ts.NewTask(); td.RegistrationInfo.Documentation = "Does something"; td.Settings.ExecutionTimeLimit = TimeSpan.Zero; //td.Principal.LogonType = TaskLogonType.InteractiveToken; // Add a cron trigger //td.Triggers.AddRange(Trigger.FromCronFormat("15 */6 */30 * *")); // Add a trigger that will fire the task at this time every other day DailyTrigger dt = (DailyTrigger)td.Triggers.Add(new DailyTrigger { DaysInterval = 2 }); dt.Repetition.Duration = TimeSpan.FromHours(4); dt.Repetition.Interval = TimeSpan.FromHours(1); // Add a trigger that will fire every week on Friday td.Triggers.Add(new WeeklyTrigger { StartBoundary = DateTime.Today + TimeSpan.FromHours(2), DaysOfWeek = DaysOfTheWeek.Friday, Enabled = false }); // Add message and email actions if (ts.HighestSupportedVersion >= new Version(1, 2)) { ShowMessageAction sm = (ShowMessageAction)td.Actions.AddNew(TaskActionType.ShowMessage); sm.Title = "title"; sm.MessageBody = "body"; EmailAction ma = new EmailAction("Subject", "*****@*****.**", "[email protected]; [email protected]", "Body", "mail.google.com") { Bcc = "*****@*****.**", Cc = "*****@*****.**" }; ma.Attachments = new object[] { (string)new TemporaryScopedFile() }; ma.HeaderFields.Add("N1", "V1"); ma.HeaderFields.Add("N2", "V2"); td.Actions.Add(ma); } // Add an action that will launch Notepad whenever the trigger fires td.Actions.Add(new ExecAction("notepad.exe", "c:\\test.log", null)); output.WriteLine(td.XmlText); Task t = ts.RootFolder.RegisterTaskDefinition(taskName, td); //, TaskCreation.CreateOrUpdate, "username", "password", TaskLogonType.Password); t.Enabled = false; System.Threading.Thread.Sleep(1000); output.WriteLine("LastTime & Result: {0} ({1:x})", t.LastRunTime == DateTime.MinValue ? "Never" : t.LastRunTime.ToString("g"), t.LastTaskResult); output.WriteLine("NextRunTime: {0:g}", t.NextRunTime); //DisplayTask(t, true); using (var dlg = new TaskOptionsEditor { Editable = true }) { dlg.Initialize(t); dlg.ShowDialog(); } // Retrieve the task, add a trigger and save it. //t = ts.GetTask(taskName); //ts.RootFolder.DeleteTask(taskName); //td = t.Definition; /*td.Triggers.Clear(); WeeklyTrigger wt = td.Triggers.AddNew(TaskTriggerType.Weekly) as WeeklyTrigger; wt.DaysOfWeek = DaysOfTheWeek.Friday; ((ExecAction)td.Actions[0]).Path = "calc.exe"; t = ts.RootFolder.RegisterTaskDefinition(taskName, td); output.WriteLine("Principal: {1}; Triggers: {0}", t.Definition.Triggers, t.Definition.Principal);*/ ts.RootFolder.DeleteTask(taskName); } catch (Exception ex) { output.WriteLine(ex.ToString()); } }