{"record":{"id":"bad2621346275fb7","repo":"peass-ng/PEASS-ng","slug":"task-definition-does-not-contain-a-version","errorCode":null,"errorMessage":"Task definition does not contain a version.","messagePattern":"Task definition does not contain a version\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"winPEAS/winPEASexe/winPEAS/TaskScheduler/Task.cs","lineNumber":1773,"sourceCode":"            private readonly XmlDocument doc;\n\n            public DefDoc(string xml)\n            {\n                doc = new XmlDocument();\n                doc.LoadXml(xml);\n            }\n\n            public Version Version\n            {\n                get\n                {\n                    try\n                    {\n                        return new Version(doc[\"Task\"].Attributes[\"version\"].Value);\n                    }\n                    catch\n                    {\n                        throw new InvalidOperationException(\"Task definition does not contain a version.\");\n                    }\n                }\n                set\n                {\n                    var task = doc[\"Task\"];\n                    if (task != null) task.Attributes[\"version\"].Value = value.ToString(2);\n                }\n            }\n\n            public string Xml => doc.OuterXml;\n\n            public bool Contains(string tag, string defaultVal = null, bool removeIfFound = false)\n            {\n                var nl = doc.GetElementsByTagName(tag);\n                while (nl.Count > 0)\n                {\n                    var e = nl[0];\n                    if (e.InnerText != defaultVal || !removeIfFound || e.ParentNode == null)","sourceCodeStart":1755,"sourceCodeEnd":1791,"githubUrl":"https://github.com/peass-ng/PEASS-ng/blob/53fb989abc2219826385683a6fee826bd6cd38d6/winPEAS/winPEASexe/winPEAS/TaskScheduler/Task.cs#L1755-L1791","documentation":"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.","triggerScenarios":"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.","commonSituations":"Loading task XML from a template/legacy system that omitted the version attribute; manual XML editing removing attributes; constructing definitions from partial XML strings.","solutions":["Add a version attribute to the root Task element: <Task version=\"1.4\" ...>","Validate the XML schema (Task Scheduler 2.0 schema) before loading it into a TaskDefinition","Use NewTask()/TaskService.NewTask(0) to create a well-formed definition instead of parsing arbitrary XML","Wrap the property access in try-catch and default to a known version (e.g. 1.2)"],"exampleFix":"// before\ndef.XmlText = File.ReadAllText(\"legacy.xml\"); // no version attribute\n// after\nvar xml = File.ReadAllText(\"legacy.xml\");\nif (!xml.Contains(\"version=\"))\n    xml = xml.Replace(\"<Task \", \"<Task version=\\\"1.4\\\" \");\ndef.XmlText = xml;","handlingStrategy":"validation","validationCode":"var doc = new XmlDocument(); doc.LoadXml(xml);\nvar ver = doc[\"Task\"]?.Attributes[\"version\"]?.Value;\nif (string.IsNullOrEmpty(ver)) throw new ArgumentException(\"Task XML missing version attribute\");","typeGuard":"bool HasVersion(System.Xml.XmlDocument d) => d[\"Task\"]?.Attributes[\"version\"] != null;","tryCatchPattern":"try { var v = def.Version; }\ncatch (InvalidOperationException) { /* treat as default version 1.2 or reject the XML */ }","preventionTips":["Always include <Task version=\"1.x\"> when authoring task XML","Schema-validate task XML before loading into a TaskDefinition"],"tags":["taskscheduler","xml","invalidoperation","schema"],"backgroundTag":"schema-validation-failed","analyzedSha":"53fb989abc2219826385683a6fee826bd6cd38d6","analyzedAt":"2026-09-02T04:25:09.259Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T11:17:12.671Z"}