peass-ng/PEASS-ng · error · InvalidOperationException

Task definition does not contain a version.

Error message

Task definition does not contain a version.

What it means

The TaskDefinition.Version property getter parses the version attribute from the task XML document. If the <Task> element lacks a version attribute (or parsing fails), the library throws this InvalidOperationException because a version is mandatory in a Task Scheduler XML definition.

Source

Thrown at winPEAS/winPEASexe/winPEAS/TaskScheduler/Task.cs:1773

            private readonly XmlDocument doc;

            public DefDoc(string xml)
            {
                doc = new XmlDocument();
                doc.LoadXml(xml);
            }

            public Version Version
            {
                get
                {
                    try
                    {
                        return new Version(doc["Task"].Attributes["version"].Value);
                    }
                    catch
                    {
                        throw new InvalidOperationException("Task definition does not contain a version.");
                    }
                }
                set
                {
                    var task = doc["Task"];
                    if (task != null) task.Attributes["version"].Value = value.ToString(2);
                }
            }

            public string Xml => doc.OuterXml;

            public bool Contains(string tag, string defaultVal = null, bool removeIfFound = false)
            {
                var nl = doc.GetElementsByTagName(tag);
                while (nl.Count > 0)
                {
                    var e = nl[0];
                    if (e.InnerText != defaultVal || !removeIfFound || e.ParentNode == null)

View on GitHub (pinned to 53fb989abc)

Solutions

  1. Add a version attribute to the root Task element: <Task version="1.4" ...>
  2. Validate the XML schema (Task Scheduler 2.0 schema) before loading it into a TaskDefinition
  3. Use NewTask()/TaskService.NewTask(0) to create a well-formed definition instead of parsing arbitrary XML
  4. Wrap the property access in try-catch and default to a known version (e.g. 1.2)

Example fix

// before
def.XmlText = File.ReadAllText("legacy.xml"); // no version attribute
// after
var xml = File.ReadAllText("legacy.xml");
if (!xml.Contains("version="))
    xml = xml.Replace("<Task ", "<Task version=\"1.4\" ");
def.XmlText = xml;
Defensive patterns

Strategy: validation

Validate before calling

var doc = new XmlDocument(); doc.LoadXml(xml);
var ver = doc["Task"]?.Attributes["version"]?.Value;
if (string.IsNullOrEmpty(ver)) throw new ArgumentException("Task XML missing version attribute");

Type guard

bool HasVersion(System.Xml.XmlDocument d) => d["Task"]?.Attributes["version"] != null;

Try / catch

try { var v = def.Version; }
catch (InvalidOperationException) { /* treat as default version 1.2 or reject the XML */ }

Prevention

When it happens

Trigger: Accessing taskDefinition.Version on a definition whose XML has no version attribute on the root Task element — e.g. hand-crafted or truncated XML loaded via TaskDefinition.XmlText or a TaskDefinition built from foreign XML.

Common situations: Loading task XML from a template/legacy system that omitted the version attribute; manual XML editing removing attributes; constructing definitions from partial XML strings.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of peass-ng/PEASS-ng@53fb989abc (2026-09-02). Data as JSON: /api/errors/bad2621346275fb7. Report an issue: GitHub.