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("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?

        asked Aug 27 '13 at 21:08    
Anna 195211    
 
 
                                                                                        
                                                                                                                    
                                                                                                 
 
                                                                     
   
                                                                                                                    
                                                                        
 |          

1 Answer                                 1

     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.

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

  1. 如何利用PowerShell完成的Windows服务器系统安全加固实践和基线检测

    0x00 前言简述 最近单位在做等保测评,由本人从事安全运维方面的工作(PS:曾经做过等保等方面的安全服务),所以自然而然的与信安的测评人员一起对接相关业务系统的检查,在做主机系统测评检查时发现了系统 ...

  2. Azure Powershell script检测登陆并部署ARM Template

    本文简单提供了一个Azure powershell脚本,能实现如下功能 Azure (China)账户是否已经登陆了,如果没登陆,会提示你登陆. 要创建的资源组是否存在,存在的话不再创建,直接部署te ...

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

    使用performance monitor 查看 每一个cpu core的cpu time: 打开performance monitor,添加 counter 如下 运行一段cpu bound 的代码 ...

  4. nginx performance monitor

    nginx performance monitor Nginx中的stub_status模块主要用于查看Nginx的一些状态信息 示例 Active connections: 2 server acc ...

  5. Orion Network Performance Monitor 软件在网络管理中的应用

    Orion Network Performance Monitor 软件在网络管理中的应用       Orion Network Performance Monitor是完全的带宽性能和故障管理软件 ...

  6. Performance Monitor采集性能数据

    Performance Monitor采集性能数据 Windows本身为我们提供了很多好用的性能分析工具,大家日常都使用过资源管理器,在里面能即时直观的看到CPU占用率.物理内存使用量等信息.此外新系 ...

  7. 实战Performance Monitor监测EnyimMemcached

    首先要将EnyimMemcached安装至Windows Performance Counters中. 将Enyim.Caching.dll复制到一个文件夹中 在命令行中进入.NET Framewor ...

  8. OA|DOAJ|Highwire press|Springeropen|Plos journal|电子印本|中国科技论文在线|arxiv|chinaxiv|MIT机构知识库|中科院机构知识库|Email alert|Citeseer|RSS|F1000 prime

    信息检索 OA:open access开放获取 金色OA:出版社主导, 开放出版,全部都可以下载. 开放论文:只有部分可以下载. 绿色OA:作者主导,发表后放在机构知识库中,排版不同,但是内容一致.E ...

  9. 【Azure微服务 Service Fabric 】在SF节点中开启Performance Monitor及设置抓取进程的方式

    前提条件 当我们观察到SF中某一个节点出现CPU不正常的情况,但是由于不能肉眼长期观察,所以可以通过开启Performance Monitor的方式来获取每一个进程的%Processer Time的方 ...

随机推荐

  1. Docker 监控实战

    如今,越来越多的公司开始使用 Docker 了,现在来给大家看几组数据: 2 / 3 的公司在尝试了 Docker 后最终使用了它 也就是说 Docker 的转化率达到了 67%,而转化市场也控制在 ...

  2. strip_tags() 函数剥去 HTML、XML 以及 PHP 的标签

    定义和用法 strip_tags() 函数剥去 HTML.XML 以及 PHP 的标签. 语法 strip_tags(string,allow) 参数 描述 string 必需.规定要检查的字符串. ...

  3. Android Spinner(级联 天气预报)

    activity_spinner.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayo ...

  4. boost库在windows下的编译和使用

    因为跨平台的原因,现在要使用到boost库,boost库非常大,现在处于摸索阶段. 首先来说boost库在window下的安装和使用. 一.下载 首先从boost官方主页http://www.boos ...

  5. QT的Paint 系统

    下面对于QT的绘制系统做一个简要说明, 这个系统主要由三部分组成,  QPainter, QPaintDevice, QPaintEngine. QPainter 是一个绘制接口类,提供绘制各种面向用 ...

  6. RxJava学习(三)

    变换 所谓变换,就是将事件序列中的对象或整个序列进行加工处理,转换成不同的事件或事件序列. 1) API 首先看一个 map() 的例子: Observable.just("images/l ...

  7. P137、面试题23:从上往下打印二叉树

    题目:从上往下打印出二叉树的每个结点,同一层的结点按照从左到右的顺序打印.例如输入如图的二叉树,则依次打印出8,6,10,5,7,9,11.(其实是按层遍历)二叉树结点的定义如下:struct Bin ...

  8. MinHash算法-复杂度待整理

    1MinHash简介 传统的hash算法只负责将原始内容尽量均匀随机地映射为一个签名值,原理上相当于伪随机数产生算法.传统hash算法产生的两个签名,如果相等,说明原始内容在一定概率下是相等的:如果不 ...

  9. 分布式java应用

    大型应用,通常会拆分为多个子系统来实现.       对Java来说,这些子系统可能部署在同一台机器的多个不同的JVM中,也可能部署在不同的机器上,但这些子系统又不是完全独立的,要相互通信来共同实现业 ...

  10. eclipse+pydev (python) 配置出错

    错误: eclipse+pydev 配置出错,就是在选择python interpreter那一步: See error log for details.com.sun.org.apache.xerc ...