简体   繁体   English

使用PowerShell脚本从性能监视器发送电子邮件警报

[英]Send email alert from Performance Monitor using PowerShell script

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). 我在性能监视器(Windows Server 2008 R2)中创建了一个警报,应该在\\处理器(_Total)\\%处理器时间超过10时触发(一个小值只是为了保证始终满足发送警报的条件)。 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. 此外,我还在Task Scheduler中创建了一个新任务,无论用户是否登录,它都将运行,并且它将以最高权限运行。 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. 程序/脚本:发送电子邮件的PowerShell脚本的路径。

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): PowerShell代码如下($ name,$ date,$ counter,$ threshold,$ value应该来自性能监视器数据收集器集警报任务属性,如上图所示):

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 = @"
    <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? 有谁知道什么可能是错的?

There are basically two things to address that should make this work for you. 基本上有两件事需要解决,这应该让这对你有用。

  1. Get the alert parameters correctly passed to your script. 获取正确传递给脚本的警报参数。
  2. Actually call the function defined in your script. 实际上调用脚本中定义的函数。

We'll start with the parameters. 我们将从参数开始。 On the Alert Task tab of your alert (pictured above), edit the Task Arguments field and replace: 在警报的“警报任务”选项卡上(如上图所示),编辑“任务参数”字段并替换:

{name}{date}{counter}{threshold}{value}

with: 有:

"{name}" "{date}" "{counter}" "{threshold}" "{value}"

Your parameters are basically being parsed as a space-separated string value, so we add double-quotes around each individual parameter token to handle values that include spaces, and we add a space between each individual parameter token so that we'll be able to tell one parameter from the next. 您的参数基本上被解析为空格分隔的字符串值,因此我们在每个单独的参数标记周围添加双引号来处理包含空格的值,并且我们在每个单独的参数标记之间添加一个空格,以便我们能够告诉下一个参数。

Then, for the action of your scheduled task (named "Processor Monitoring") you have to tell it to expect parameters from the alert and to pass those parameters to the PowerShell script. 然后,对于计划任务的操作(名为“处理器监视”),您必须告诉它预期警报中的参数并将这些参数传递给PowerShell脚本。

在此输入图像描述

Your Action is correct, ie "Start a program". 你的行动是正确的,即“开始一个程序”。

For the Program/script field, enter "powershell.exe" (or browse for the full path). 对于Program / script字段,输入“powershell.exe”(或浏览完整路径)。

And for the Add Arguments field, enter this: 对于Add Arguments字段,输入以下内容:

-File C:\path\to\your\PowerShell\scripts\perfmon_send_email.ps1 $(Arg0)

Where perfmon_send_email.ps1 is the script file containing your SendMail() function as described above. 其中perfmon_send_email.ps1是包含SendMail()函数的脚本文件,如上所述。

This bit was kind of finicky, so there may be other ways to set this up, but explicitly using the -File parameter made a difference for my tests. 这个位有点挑剔,所以可能有其他方法来设置它,但显式使用-File参数对我的测试产生了影响。 The $(Arg0) part is what gets replaced with the parameter string from the Alert when the scheduled task executes PowerShell to run your script. 当计划任务执行PowerShell以运行脚本时, $(Arg0)部分将被Alert中的参数字符串替换。

So that should make the Alert parameters available to your PowerShell script. 因此,应该使PowerShell脚本可以使用Alert参数。 Now all you have to do is actually call the function you've already defined. 现在你所要做的就是调用你已定义的函数。 Add the following to the end of your script (after the function definition): 将以下内容添加到脚本的末尾(在函数定义之后):

# Get parameter values by position and pass them to the SendMail() function.
SendMail $args[0] $args[1] $args[2] $args[3] $args[4]

$args is an array containing the parameter values passed to a script file called from the command line, which is exactly what we configured the scheduled task to do. $args是一个数组,包含传递给从命令行调用的脚本文件的参数值,这正是我们配置要执行的计划任务的内容。

Since we know the alert will always send the same values in the same order, ie name, date, counter, threshold, and value, we can just pull them from the command line arguments based on position and pass them to the SendMail() function. 由于我们知道警报将始终以相同的顺序发送相同的值,即名称,日期,计数器,阈值和值,我们可以根据位置从命令行参数中提取它们并将它们传递给SendMail()函数。

Note that there are more robust ways to process command line arguments, but this should be sufficient for your purposes. 请注意,有更强大的方法来处理命令行参数,但这应该足以满足您的需要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM