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. HDU1431+简单题

    题意简单 预处理之后会发现符合条件的数最多781个... 所以打表.. /* */ #include<algorithm> #include<iostream> #includ ...

  2. android 在一个scrollView里面嵌套一个需要滑动的控件(listView、gridView)

    package cn.via.dageeeOrderFood.widget; import android.content.Context; import android.graphics.Point ...

  3. tomcat 设置默认编码格式

    在tomcat目录下 conf文件夹下的server.xml中: <Connector port="80" protocol="HTTP/1.1"     ...

  4. 修改jmeter jvm参数

    记录下常用的linux下 jmeter jvm参数修改,打开jmeter安装目录/bin/jmeter(非jmeter.sh) 1. 修改默认堆内存大小 #默认的 HEAP="-Xms512 ...

  5. 【BZOJ 2618】 2618: [Cqoi2006]凸多边形 (半平面交)

    2618: [Cqoi2006]凸多边形 Description 逆时针给出n个凸多边形的顶点坐标,求它们交的面积.例如n=2时,两个凸多边形如下图: 则相交部分的面积为5.233. Input 第一 ...

  6. [itint5]任务调度

    http://www.itint5.com/oj/#10 拓扑排序.首先按照题目给出的数据结构复杂度不会是O(v+e)的,所以先要变换数据结构.二来写的时候用一个stack会更好点.还有就是题目里其实 ...

  7. 在其他的电脑上配置绿色Jre+tomcat运行环境

    其他的同事要使用我们的web程序(基于tomcat的web程序).所以要求是对方的电脑任何程序都不需要安装,把我们的包拷贝过去,直接执行批处理就可以运行. 经过了一番摸索,实现方式如下: 1,准备jr ...

  8. 174. Dungeon Game

    题目: The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dung ...

  9. C语言字符串查找函数

    #include <string.h> #include <stdio.h> char * string_search(char long_str[], char short_ ...

  10. nodejs创建express+ejs项目

    很多同学跟着 node.js开发指南 学习用 express+ejs 创建项目的时候,却总是无法成功,如: 运行 以下命令去创建 express+ejs 创建项目 $ express -t ejs m ...