Powershell script for memory usage by process in desc order

This topic contains 6 replies, has 2 voices, and was last updated by  M1981 1 year, 7 months ago.

  • Author
  • #58900


    Hi Everyone,

    Checking if there is any script handy which displays top 10 memory consuming processes in HTML format for given list of Servers.
    Also, would like to know the differences between below. Any example or analogy would be a great help.
    Last but not least, to get overall memory consumption of process what are things we need consider? is it only Working Set alone or any other thing we need to take into consideration?

    -WorkingSet Shareable

    Thank you.

  • #58902

    Olaf Soyk

    If you're looking for prewritten scripts you can try it here: Microsoft Technet Script Gallery

    • #58914


      Thanks Olaf.

      I searched and I got a script. Now I need help on formatting the o/p. currently this works for only 1 server. it returns two results. Result1 – process working set output and Result2 is the piechart of top 5 processes and the image is stored in D:\images\ folder. My requirement is, I need a HTML report which shows above 2 results in a HTML Format as an email body.

      Got script from : https://gallery.technet.microsoft.com/scriptcenter/Charting-Pie-Chart-using-2579fa0d

      $ErrorActionPreference = "SilentlyContinue"

      # result1 as table
      $Processes = Get-Process | Sort-Object -Property WS -Descending| Select-Object Name,WS,PM,VM -first 5
      $Processes|Format-Table -AutoSize

      # * * * * * * * * * * * * Physical Memory Usage: Top 5 Processes* * * * * * * * * * * * * * * * * * * * * * * *
      #result2 — pie chart
      $scriptpath = Split-Path -parent $MyInvocation.MyCommand.Definition
      $MemoryUsageChart1 = New-object System.Windows.Forms.DataVisualization.Charting.Chart
      $MemoryUsageChart1.Width = 800
      $MemoryUsageChart1.Height = 400
      $MemoryUsageChart1.BackColor = [System.Drawing.Color]::White

      [void]$MemoryUsageChart1.Titles.Add("Physical Memory Usage: Top 5 Processes")
      $MemoryUsageChart1.Titles[0].Font = "segoeuilight,20pt"
      $MemoryUsageChart1.Titles[0].Alignment = "topLeft"
      $chartarea = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea
      $chartarea.Name = "ChartArea1"
      $MemoryUsageChart1.Series["data1"].ChartType = [System.Windows.Forms.DataVisualization.Charting.SeriesChartType]::Pie
      $Processes = Get-Process | Sort-Object -Property WS | Select-Object Name,PM,VM -Last 5
      $ProcessList = @(foreach($Proc in $Processes){$Proc.Name + "`n"+[math]::floor($Proc.PM/1MB)})
      $Placeholder = @(foreach($Proc in $Processes){$Proc.PM})
      $MemoryUsageChart1.Series["data1"].Points.DataBindXY($ProcessList, $Placeholder)

  • #58921

    Olaf Soyk

    This isn't the place to ask others to write or rewrite/redesign scripts for you. Especially not for scripts you found on the internet. We're happy to answer specific questions, but it's not helpful to you for someone to simply do it all for you.

    I recommend for you to learn Powershell. I believe it will pay off for you in future. Here you can find some great starting points: Beginner Sites And Tutorials

    • #58965


      Okay. Thank you.

  • #58971


    Hi All,

    I tried below code on my own. But the output is not as expected. Could anyone tell me why the value and image file stored in variable $body is not rendering in outlook email message? In my email message we should see the Get-Process table along with an win10.jpg image embedded in the email which is not at present. Not sure why.

    Can anybody help? Am I missing anything here?

    Add-PSSnapin Microsoft.Exchange.Management.Powershell.Admin -erroraction silentlyContinue
    $smtpServer = "smtp.gmail.com"
    $att = new-object Net.Mail.Attachment("D:\images\win10.jpg")
    $att.ContentId = "att"
    $msg = new-object Net.Mail.MailMessage
    $msg.From = "abc@gmail.com"

    $smtp = new-object Net.Mail.SmtpClient($smtpServer,587)
    $smtp.EnableSsl = $true
    $smtp.Credentials = New-Object System.Net.NetworkCredential("abc@gmail.com", "pwd#125")

    $Processes = Get-Process | Sort-Object -Property WS -Descending| Select-Object Name,WS,PM,VM -first 5

    $body = $Processes
    $body = $body + " Hello World "
    $body | ConvertTo-HTML| Out-File "d:\images\Report.html"

    $msg.Subject = "Picture Powershell " + (get-date -format yyyyMMdd)
    $msg.Body = $body
    $msg.IsBodyHTML = $true
    Invoke-Expression "d:\images\Report.html"

    Thanks in advance.

  • #58975


    To make things simpler, used below code to embed .png image to email. Still not working!!!
    In my outlook it just shows a small thumbnail and the not image. Any thoughts please???

    $body = "

    img src='cid:Attachment' />


    $smtpserver = "smtp.gmail.com"
    $smtp = new-object Net.Mail.SmtpClient($smtpserver,587)
    $smtp.EnableSsl = $true
    $smtp.Credentials = New-Object System.Net.NetworkCredential("abc@gmail.com", "pwd#125")

    $image = "D:\images\win10.png"
    $attachment = new-object Net.Mail.Attachment($image)
    $attachment.ContentType.MediaType = “image/png”
    $attachment.ContentId = "Attachment"
    $msg = new-object Net.Mail.MailMessage
    $msg.From = "abc@gmail.com"
    $msg.IsBodyHTML = $true
    $msg.Body = $body
    $msg.Subject = "Image from PowerShell " + (get-date -format yyyyMMdd)

You must be logged in to reply to this topic.