Send email alert from Performance Monitor using PowerShell script (檢測windows伺服器的cpu 硬碟 服務等效能,發email的方法) -摘自網路

來源:互聯網
上載者:User

標籤:

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.

Send email alert from Performance Monitor using PowerShell script (檢測windows伺服器的cpu 硬碟 服務等效能,發email的方法) -摘自網路

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.