Send email alert from Performance Monitor using PowerShell script (检测windows服务器的cpu 硬盘 服务等性能,发email的方法) -摘自网络

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?

email powershell alert windows-server-2008-r2 performance-monitor


share|improve this question

edited Aug 29 ‘13 at 16:10

asked Aug 27 ‘13 at 21:08

Anna 195211

 

 
                                                                                        
                                                                                                                    

                                                                                                 
 
                                                                     
   
                                                                                                                    

                                                                        

|          

1 Answer                                 1

activeoldestvotes


     up vote4down voteaccepted

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.

Your Action is correct, i.e. "Start a program".

For the Program/script field, enter "powershell.exe" (or browse for the full path).

And for the Add Arguments field, enter this:

-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.

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.  The $(Arg0) part is what gets replaced with the parameter string from the Alert when the scheduled task executes PowerShell to run your script.

So that should make the Alert parameters available to your PowerShell script.  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.

Since we know the alert will always send the same values in the same order, i.e. 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.

Note that there are more robust ways to process command line arguments, but this should be sufficient for your purposes.

时间: 2024-10-16 17:54:32

Send email alert from Performance Monitor using PowerShell script (检测windows服务器的cpu 硬盘 服务等性能,发email的方法) -摘自网络的相关文章

使用performance monitor 查看 每一个cpu core的cpu time

使用performance monitor 查看 每一个cpu core的cpu time: 打开performance monitor,添加 counter 如下 运行一段cpu bound 的代码: cpu time瞬间彪到100% 结束代码运行.

Performance Monitor Usage6:Side Effect

开启Performance Monitor 会不会对Server 性能有影响? 在一个Production Server上,运行一段时间之后,在Task Management 中长款Performance Monitor消耗的资源 引用<SQL Server 2012 实施与管理实战指南>来解释我的疑惑: 默认的Performance Monitor 是用来Real-Time 检测系统的,在现实的问题分析中,更多的是需要在问题发生之前,开启Performance Counter的数据收集,一段

Performance Monitor Usage2:Peformance Counter

Performance Counter 是量化系统状态或活动的一个数值,对于不同的监控目的,需要设置不同的Performance Counter. Performance counters   are measurements of system state or activity. They can be included in the operating system or can be part of individual applications. Windows Performance

nginx performance monitor

nginx performance monitor Nginx中的stub_status模块主要用于查看Nginx的一些状态信息 示例 Active connections: 2 server accepts handled requests 12055 12055 34907 Reading: 0 Writing: 1 Waiting: 1 这些状态信息包括 1. Active connections: 对后端发起的活动连接数. 2. server accepts handled reques

Performance Monitor Usage8:An Practice

Performance Monitor provides a visual display of built-in Windows performance counters, either in real time or as a way to review historical data. You can add performance counters to Performance Monitor by dragging and dropping, or by creating custom

使用PowerShell收集多台服务器的性能计数器

写在前面     当管理多台Windows Server服务器时(无论是DB.AD.WEB以及其他的应用服务器),当出现性能或其他问题后,参阅性能计数器都是一个非常好的维度从而推测出问题可能出现的原因,再不济也能缩小需要考虑的问题范围,因此定期收集每一台服务器的计数器就会使得问题有据可循.并且收集到的数据也可以作为BaseLine,即使没有出现问题也可以预先判断一些问题.     之前看到网上的大多数收集性能计数器的文章都比较局限,一般是只收集单台服务器,因此我分享一个多服务器的写法.    

Use powershell script against password hacking over OWA

In my previous company Exchange OWA isn't published to internet, so this blog described my first time encountering hacker trying to hack my new company's Active directory passwords. Based on it, I wrote a powershell script running on each CAS servers

利用Velocity结合Spring发email

在spring中发mail是一件容易的事,如果利用Velocity做mail的模板来发送就更得心应手了. 首先,还是简单描述sping中的配置,发mail需要一个mail的engin: <bean id="mailEngine" class="com.aodragon.jdt.backend.service.impl.MailEngineImpl"> <property name="mailSender"> <ref

通过Powershell远程管理Windows Azure上的虚拟机

Windows Azure上连接虚拟机想必不是件难事尤其是连接Windows操作系统简单点几下鼠标通过远程桌面RDPWindows Azure虚拟机会帮助你自动创建远程连接RDP的Profile你就能体验到公有云带来的便利.由于虚拟机外部连接都是通过端口映射连接的.当然基于区域网络目前Azure.CN中新创建的虚拟网络已经都是区域网络了当然你目前仍然可以创建基于地缘组的虚拟网络虽然并不推荐后可以创建虚拟机实例级别的公共IP地址所以你也可以跳过通过"云服务"端口映射的RDP而直接连接虚拟