peass-ng/PEASS-ng · error · System.InvalidOperationException

Under Windows 8 and later, EmailAction objects are converted

Error message

Under Windows 8 and later, EmailAction objects are converted to PowerShell. This action contains headers that are not supported.

What it means

Under Windows 8+, the library converts EmailAction output to a PowerShell script (since native email actions are deprecated). Custom headers other than the recognized Importance header cannot be represented in that conversion, so if unsupported headers remain the library throws InvalidOperationException instead of silently dropping them.

Source

Thrown at winPEAS/winPEASexe/winPEAS/TaskScheduler/Action.cs:660

                    sb.AppendFormat(" -Cc {0}", ToPS(Cc));
                if (!string.IsNullOrEmpty(Bcc))
                    sb.AppendFormat(" -Bcc {0}", ToPS(Bcc));
                if (bodyIsHtml)
                    sb.Append(" -BodyAsHtml");
                if (!string.IsNullOrEmpty(Body))
                    sb.AppendFormat(" -Body '{0}'", ToUTF8(Prep(Body)));
                if (Attachments != null && Attachments.Length > 0)
                    sb.AppendFormat(" -Attachments {0}", ToPS(Array.ConvertAll(Attachments, o => Prep(o.ToString()))));
                var hdr = new List<string>(HeaderFields.Names);
                if (hdr.Contains(ImportanceHeader))
                {
                    var p = Priority;
                    if (p != System.Net.Mail.MailPriority.Normal)
                        sb.Append($" -Priority {p}");
                    hdr.Remove(ImportanceHeader);
                }
                if (hdr.Count > 0)
                    throw new InvalidOperationException("Under Windows 8 and later, EmailAction objects are converted to PowerShell. This action contains headers that are not supported.");
                sb.Append("; ");
                return sb.ToString();

                /*var msg = new System.Net.Mail.MailMessage(this.From, this.To, this.Subject, this.Body);
                if (!string.IsNullOrEmpty(this.Bcc))
                    msg.Bcc.Add(this.Bcc);
                if (!string.IsNullOrEmpty(this.Cc))
                    msg.CC.Add(this.Cc);
                if (!string.IsNullOrEmpty(this.ReplyTo))
                    msg.ReplyTo = new System.Net.Mail.MailAddress(this.ReplyTo);
                if (this.Attachments != null && this.Attachments.Length > 0)
                    foreach (string s in this.Attachments)
                        msg.Attachments.Add(new System.Net.Mail.Attachment(s));
                if (this.nvc != null)
                    foreach (var ha in this.HeaderFields)
                        msg.Headers.Add(ha.Name, ha.Value);
                var client = new System.Net.Mail.SmtpClient(this.Server);
                client.Send(msg);*/

View on GitHub (pinned to 53fb989abc)

Solutions

  1. Remove unsupported custom headers from the EmailAction's Headers collection before conversion/registration
  2. Keep only supported headers (e.g. Importance) and move custom header needs into the email body or the PowerShell script itself
  3. Replace the EmailAction with an explicit PowerShell action (Send-MailMessage / .NET SmtpClient) that supports arbitrary headers
  4. Catch InvalidOperationException during conversion and warn that the action cannot be represented on Windows 8+

Example fix

// before
emailAction.Headers.Add("X-Custom-Trace", "abc123");
var ps = emailAction.ToString(); // throws on Win8+
// after
// X-Custom-Trace cannot be represented; embed it in the body or use a PowerShell action
emailAction.Body = "Trace: abc123";
var ps = emailAction.ToString();
Defensive patterns

Strategy: try-catch

Validate before calling

static bool HasUnsupportedHeaders(EmailAction a) =>
    a.Headers.Keys.Cast<string>().Any(h =>
        h != "Importance" && h.StartsWith("X-", StringComparison.OrdinalIgnoreCase));

Try / catch

try { var ps = emailAction.ToString(); }
catch (InvalidOperationException)
{
    log.Warn("Email action has headers unsupported by PowerShell conversion (Win8+); replacing with explicit PowerShell action.");
}

Prevention

When it happens

Trigger: Reading ToString/PowershellAction conversion of an EmailAction whose Headers collection contains entries other than the standard headers (and ImportanceHeader) — typically after loading a V1 task XML that specified custom mail headers.

Common situations: Migrating Windows 7 era scheduled tasks with custom email headers to Windows 8/10/11; importing third-party task definitions that set X- custom headers; user-added headers expecting them to be sent.

Related errors


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