none
send mailmessage after a if statement RRS feed

  • Question

  • Using Powershell on Windows 7 - I'm trying to check a process for a specific CPU Utilization and then shut the process down after it hits the specified CPU Utilization and send an email notifying the process has been shut down.  I can not get the script to send an email ONLY when it hits the CPU usage.   The script is going to check the process about every 5 minutes from Task Scheduler and if the CPU does not hit a specific CPU Utilization, I do not want the email message going out.  AS of right now my testing with the script below works but it sends the email message regardless if CPU hits the specified utilization on the process.  I want to know if there is a way to script the "send-mailmessage" after an if statement to further specify the condition for when to send the email?    Any help would be appreciated. 

     

    Get-Process Notepad* | Where { $_.cpu -gt .15 }
    Send-MailMessage –To email address –From  email address –SmtpServer smtp.xyz.com –Subject “Notepad - CPU Utilization has been checked for 80% Threshold” –Body “Notepad CPU Utilization has been checked for a cpu_threshold of `80%” 
    Stop-process -processname Notepad*

    Friday, February 14, 2014 8:12 PM

Answers

  • $var = Get-Process | Where { $_.cpu -gt .15  -and $_.processname -like "notepad*"} 

    if ($var.count -gt 0)

    {

    Send-MailMessage –To email address –From  email address –SmtpServer smtp.xyz.com –Subject “Notepad - CPU Utilization has been checked for 80% Threshold” –Body “Notepad CPU Utilization has been checked for a cpu_threshold of `80%” 

    Stop-process -processname Notepad* 

    }

    this only triggers if get-process returns a result where the process is named notepad* and cpu is greater than 15

    • Marked as answer by CKepley Friday, February 14, 2014 9:54 PM
    Friday, February 14, 2014 8:23 PM

All replies

  • Hi,

    Try this:

    $processToCheck = Get-Process notepad -ErrorAction SilentlyContinue | Where { $_.CPU -gt .15 }
    
    If ($processToCheck) { 
    
        Send-MailMessage
        Stop-Process
        Anything Else You Want To Do
        
    }


    Don't retire TechNet! - (Don't give up yet - 12,575+ strong and growing)

    Friday, February 14, 2014 8:19 PM
  • CPU usage is a changing value.  To see historical usage you need to use perfmon counters.  Notepad would never get to 15 % under any circumstances.

    ¯\_(ツ)_/¯

    • Marked as answer by CKepley Friday, February 14, 2014 9:54 PM
    • Unmarked as answer by CKepley Friday, February 14, 2014 9:54 PM
    Friday, February 14, 2014 8:21 PM
  • $var = Get-Process | Where { $_.cpu -gt .15  -and $_.processname -like "notepad*"} 

    if ($var.count -gt 0)

    {

    Send-MailMessage –To email address –From  email address –SmtpServer smtp.xyz.com –Subject “Notepad - CPU Utilization has been checked for 80% Threshold” –Body “Notepad CPU Utilization has been checked for a cpu_threshold of `80%” 

    Stop-process -processname Notepad* 

    }

    this only triggers if get-process returns a result where the process is named notepad* and cpu is greater than 15

    • Marked as answer by CKepley Friday, February 14, 2014 9:54 PM
    Friday, February 14, 2014 8:23 PM
  • CPU usage is a changing value.  To see historical usage you need to use perfmon counters.  Notepad would never get to 15 % under any circumstances.

    I forced it to run that high (I got up to 24%) for testing by opening up a 100MB registry export.

    Don't retire TechNet! - (Don't give up yet - 12,575+ strong and growing)


    Friday, February 14, 2014 8:24 PM
  • I think the condition in your where clause is not doing what you expect.  The CPU column returned by get-process is total processor time in seconds.  I think you need to analyze the values a bit more and then reevaluate your where condition.
    Friday, February 14, 2014 8:24 PM
  • I think the condition in your where clause is not doing what you expect.  The CPU column returned by get-process is total processor time in seconds.  I think you need to analyze the values a bit more and then reevaluate your where condition.

    =]

    That's what I get for not reading....


    Don't retire TechNet! - (Don't give up yet - 12,575+ strong and growing)

    Friday, February 14, 2014 8:29 PM
  • What you are looking for can only be read from the performance monitor counters.

    ¯\_(ツ)_/¯

    Friday, February 14, 2014 9:17 PM