Git Product home page Git Product logo

Comments (3)

lancehilliard avatar lancehilliard commented on June 1, 2024

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.

lancehilliard avatar lancehilliard commented on June 1, 2024

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.

dahall avatar dahall commented on June 1, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.