Comments (3)
Here's the full code I'm using, with TaskScheduler 2.7.2 installed from NuGet. Note the location of the following comment in the shared code:
// "line 59" cited in stacktrace above
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using Microsoft.Win32.TaskScheduler;
namespace ScheduledTasks.ConsoleApp {
internal class Program {
private static readonly string Domain = "contoso";
private static readonly string RootPath = @"\";
private static void Main() {
var serverNames = new List<string>{"host1", "host2", "host3", "etc"}.OrderBy(x=>x).ToList();
Output($"Running on {Environment.MachineName}. Querying {serverNames.Count} servers:");
Output($"{string.Join(", ", serverNames)}");
Output();
EnumAllTasks(serverNames);
}
private static void EnumAllTasks(IEnumerable<string> serverNames) {
foreach (var serverName in serverNames) {
try {
using (var ts = new TaskService(serverName)) {
EnumFolderTasks(ts.RootFolder);
}
}
catch (Exception e) {
var errorMessage = $"{serverName} - Exception: {e.Message}";
Output(errorMessage, true);
Output(isError: true);
}
}
}
private static void EnumFolderTasks(TaskFolder taskFolder) {
bool IsRelevant(Task x) {
var isEnabled = x.Enabled;
var isInRootFolder = x.Folder.Path.Equals(RootPath);
var hasDomainUser = x.Definition.Principal.UserId?.Contains(Domain) ?? false; // "line 59" cited in stacktrace above
var hasExecActions = x.Definition.Actions.OfType<ExecAction>().Any();
return isEnabled && isInRootFolder && hasDomainUser && hasExecActions;
}
foreach (var task in taskFolder.AllTasks.Where(IsRelevant)) { Print(task); }
foreach (var subFolder in taskFolder.SubFolders) { EnumFolderTasks(subFolder); }
}
private static void Print(Task t) {
Output($@"\\{t.TaskService.TargetServer ?? Environment.MachineName}{t.Path}\{t.Name}:");
foreach (var execAction in t.Definition.Actions.OfType<ExecAction>()) {
Output($"Path: {execAction.Path}");
Output($"Args: {execAction.Arguments}");
}
foreach (var trigger in t.Definition.Triggers) {
var triggerDescriptor = "When: ";
if (trigger is WeeklyTrigger weeklyTrigger) {
triggerDescriptor = $"{triggerDescriptor}{weeklyTrigger.DaysOfWeek} ";
}
triggerDescriptor = $"{triggerDescriptor}@ {trigger.StartBoundary.ToShortTimeString()}";
if (trigger.Repetition.IsSet()) {
triggerDescriptor = $"{triggerDescriptor} (Interval: {trigger.Repetition.Interval}; Duration: {trigger.Repetition.Duration})";
}
Output(triggerDescriptor);
}
Output($"Service Account: {t.Definition.Principal.UserId}");
Output("Reason: ");
Output();
}
private static void Output(string message = null, bool isError = false) {
var timeDescriptor = Process.GetCurrentProcess().StartTime.ToString("yyyyMMddHHmmss");
var categoryDescriptor = isError ? "errors" : "info";
File.AppendAllText($"{timeDescriptor}-{categoryDescriptor}.txt", $"{message}{Environment.NewLine}");
Console.WriteLine(message);
}
}
}
from taskscheduler.
I resolved this by running the same code from a Windows Server 2012 R2 machine. All of my hosts query successfully without error. Please close this issue whenever it suits you.
from taskscheduler.
You figured it out. An down-level machine with an older version of the native Task Scheduler library will have problems when accessing up-level tasks. Unfortunately this is a native library problem and this wrapper library is unable to cover it up.
from taskscheduler.
Related Issues (20)
- Add an Example for TaskService.FindAllTasks Method (Predicate(Task), Boolean)
- Add an Example for TaskPrincipal Class
- Add an Example for TriggerCollection.Remove Method
- TaskScheduler 1,000 Stars. HOT 1
- Task is not created if try with different user HOT 1
- Please update the task security page with info about LocalAccountTokenFilterPolicy HOT 1
- TaskService stores a connection password in clear text HOT 1
- Add an Example for CustomTrigger Class
- Cannot create Task within a AWS EC2 machine HOT 2
- problem with Task when starting from a service HOT 4
- Add an Example for RepetitionPattern Class HOT 1
- Task Fails to run under different user account HOT 5
- Task Does not Create on French OS Computer to English Computer HOT 2
- Using task scheduler with System.Runtime.InteropServices in .NET FRAMEWORK 4.5
- Task Updation fails HOT 2
- Add an Example for TaskService.FindTask Method HOT 1
- Support For Clustered Tasks ? HOT 1
- Does not register task in executable. HOT 1
- `TaskDefinition.Settings.Priority` default value NOT `Normal` HOT 1
- UnauthorizedAccessException without credentials HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from taskscheduler.