Send email alert from performance Monitor using PowerShell script (to detect performance such as CPU HDD service for Windows Server, email method)-from the network

Source: Internet
Author: User
Tags mailmessage smtpclient

I have created a alert in performance Monitor (Windows Server R2) This should be triggered whenever \processor (_tota L) \% Processor time is Above (a small value, just to guarantee, the condition for sending the alert was always met). You can see the Alert Task properties in the image.

In addition, I has also created a new task in the task Scheduler that would run whether the user is logged on or not, and It'll 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 are 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 was supposed to come from the Performa NCE Monitor Data Collector Set Alert task properties, as in the image above):

  function SendMail ($name, $date, $counter, $threshold, $value) {$MailMessage = New-object Net.Mail.MailMessag E $MailMessage. To.add ("[email protected]") $MailMessage. From = "[email protected]" $MailMessage. Subject = " Alert-performance Monitor "$MailMessage. isbodyhtml = $True $MailMessage. Body = @"  

Once The task is started, I has the following in the History:task started, Action started, and Created ask Process. The email is never sent though.

I tried sending an e-mail using the Action:send an email, and it worked fine. Does anyone know what is could be wrong?

Email PowerShell alert Windows-server-2008-r2 Performance-monitor
Share|improve this question Edited ' at 16:10 Asked ' at 21:08 Anna 195 2
  < Span class= "comment-date" dir= "ltr" >            < Span class= "Edited-yes" title= "This comment was edited 1 time" >                                                                                
                                                                                                                          
                      < Span class= "Edited-yes" title= "This comment was edited 1 time" >                                                                                
           ;                                                                                                               
 |
1 Answer1Activeoldestvotes
Up vote4down voteaccepted

There is basically, things to address, should make, this.

    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 is basically being parsed as a space-separated string value, so we add double-quotes around each Individu Al parameter token to handle values this include spaces, and we add a space between each individual parameter tokens so tha T we'll be able-to-tell-one parameter from the next.

Then, for the action of your scheduled task (named "Processor Monitoring") we have a to-tell it to expect parameters from T He 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 is the perfmon_send_email.ps1 script file containing your SendMail () function as described above.

This bit is kind of finicky, so there may is other ways to set this up, but explicitly using the -File parameter made a D  Ifference for my tests. The replaced with the parameter string from the Alert when the $(Arg0) scheduled task executes PowerShell To run your script.

So, should make the Alert parameters available to your PowerShell script.  Now, all of you are 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]

$argsis an array containing the parameter values passed to a script file called from the command line, which are exactly what we Configured the scheduled task to do.

Since We know the alert would 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 is more robust ways to process command line arguments, and this should is sufficient for your purposes.

Send email alert from performance Monitor using PowerShell script (to detect performance such as CPU HDD service for Windows Server, email method)-from the network

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.