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.
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:
- Begin the task: On an event
- Settings: Basic
- Log: System
- Source: Processor
The Actions (and this is the part I don't know if it's correct) has the following settings:
- Action: Start a program
- Program/script: the path to a PowerShell script to send an email.
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):
function SendMail ($name, $date, $counter, $threshold, $value) {
$MailMessage = New-Object Net.Mail.MailMessage
$MailMessage.To.Add("[email protected]")
$MailMessage.From = "[email protected]"
$MailMessage.Subject = "ALERT - Performance Monitor"
$MailMessage.IsBodyHtml = $True
$MailMessage.Body = @"
<html><head></head><body>
The following counter needs attention:<BR><BR>
Name: $($name)<BR>
Date: $($date)<BR>
Counter: $($counter)<BR>
Threshold: $($threshold)<BR>
Actual Value: $($value)<BR>
<FONT face=Courier>$($html)</FONT>
<BR>
--- Automatically generated with SENDMAIL function ---
</body>
</html>
"@
$SmtpClient = New-Object Net.Mail.SmtpClient("blah.bleh")
$SmtpClient.Send($MailMessage)
}
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.
I tried sending an email using the Action: Send an email, and it worked fine. Does anyone know what could be wrong?