title: Send email alert from Performance Monitor using PowerShell script
body: <p>I have created an alert in Performance Monitor (Windows Server 2008 R2) that should be triggered whenever \Processor(_Total)\% Processor Time is Above 10 (a small value just to guarantee that the condition for sending the alert is always met). You can see the Alert Task properties in the image. </p>
  
  <p><img src="https://i.stack.imgur.com/PbPxj.jpg" alt="enter image description here"></p>
  
  <p>In addition, I have also created a new task in the Task Scheduler that will run whether the user is logged on or not, and it will run with highest privileges. The trigger for this task has the following properties:</p>
  
  <ul>
  <li>Begin the task: On an event</li>
  <li>Settings: Basic</li>
  <li>Log: System</li>
  <li>Source: Processor</li>
  </ul>
  
  <p>The Actions (and this is the part I don't know if it's correct) has the following settings: </p>
  
  <ul>
  <li>Action: Start a program</li>
  <li>Program/script: the path to a PowerShell script to send an email.</li>
  </ul>
  
  <p>The PowerShell code is the following ($name, $date, $counter, $threshold, $value are supposed to come from the Performance Monitor data collector set alert task properties, as in the image above):</p>
  
  <pre><code>function SendMail ($name, $date, $counter, $threshold, $value) {
    $MailMessage = New-Object Net.Mail.MailMessage
    $MailMessage.To.Add("myemail@blah.bleh")
    $MailMessage.From = "do-not-reply@blah.bleh"
    $MailMessage.Subject = "ALERT - Performance Monitor"
    $MailMessage.IsBodyHtml = $True
  
    $MailMessage.Body = @"
      &lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;
      The following counter needs attention:&lt;BR&gt;&lt;BR&gt;
      Name: $($name)&lt;BR&gt;
      Date: $($date)&lt;BR&gt;
      Counter: $($counter)&lt;BR&gt;
      Threshold: $($threshold)&lt;BR&gt;
      Actual Value: $($value)&lt;BR&gt;
      &lt;FONT face=Courier&gt;$($html)&lt;/FONT&gt;
      &lt;BR&gt;
      --- Automatically generated with SENDMAIL function ---
      &lt;/body&gt;
      &lt;/html&gt;
  "@
  
    $SmtpClient = New-Object Net.Mail.SmtpClient("blah.bleh")
    $SmtpClient.Send($MailMessage)
  }
  </code></pre>
  
  <p>Once the task is started, I have the following in the History: Task Started, Action Started, and Created ask Process. The email is never sent though.</p>
  
  <p>I tried sending an email using the Action: Send an email, and it worked fine. Does anyone know what could be wrong?</p>