title: Powershell: Monitor CPU and RAM percentage usage and send email alert
body: <p>I need to do a monitor for cpu and ram usage but about percentage in Powershell.
  I tried to do some things but it is weird.
  The code has two parts and I need to combine these parts and be able to send an email.
  My big problem is about email when it is not legible.</p>
  <p>First part:</p>
  <pre><code>$repeat_count = 3
  $cpu_threshold = 80
  $sleep_interval = 5
  $hit = 0
  foreach($turn in 1..$repeat_count) {
  $cpu = (gwmi -class Win32_Processor).LoadPercentage
  #write-host “CPU is $cpu`%”
  If($cpu -ge $cpu_threshold) {
  $hit = $hit+1
  }
  start-sleep $sleep_interval
  }
  
  if($hit -eq 3) {
  write-host “CPU is more then $cpu_threshold`%”
  
  Send-MailMessage -From user01@fabrikam.com -To user02@fabrikam.com -Subject “CPU2 is more then $cpu_threshold`%” -Body “CPU is $cpu`%” -Encoding UTF8 -SmtpServer smtp@fabrikam.com
  
  } else {
  write-host “CPU is under the limit”
  }  
  </code></pre>
  <p>Second part:</p>
  <pre><code>Get-Counter -ErrorAction SilentlyContinue '\Process(*)\% Processor Time' | Select-Object -ExpandProperty countersamples| Select-Object -Property instancename, cookedvalue| ? {$_.instanceName -notmatch &quot;^(idle|_total|system)$&quot;} | Sort-Object -Property cookedvalue -Descending| Select-Object -First 5| ft InstanceName,@{L='CPU';E={($_.Cookedvalue/100/$env:NUMBER_OF_PROCESSORS).toString('P')}} -AutoSize
  </code></pre>
  <p>Can You help me how I can send an email with legible -body?
  Thank You very much</p>