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:
The Actions (and this is the part I don't know if it's correct) has the following settings:
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):
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.
We'll start with the parameters. On the Alert Task tab of your alert (pictured above), edit the Task Arguments field and replace:
with:
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:
Where This bit was kind of finicky, so there may be other ways to set this up, but explicitly using the 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):
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. |