Powershell script for memory usage by process in desc order

This topic contains 6 replies, has 2 voices, and was last updated by Profile photo of M1981 M1981 6 days, 20 hours ago.

  • Author
    Posts
  • #58900
    Profile photo of M1981
    M1981
    Participant

    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
    -PrivateWorkingSet
    -WorkingSet Shareable

    Thank you.

  • #58902
    Profile photo of Olaf Soyk
    Olaf Soyk
    Participant

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

    • #58914
      Profile photo of M1981
      M1981
      Participant

      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

      Code:
      $ErrorActionPreference = "SilentlyContinue"
      clear-host

      # 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
      [void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms.DataVisualization")
      $scriptpath = Split-Path -parent $MyInvocation.MyCommand.Definition
      #frame
      $MemoryUsageChart1 = New-object System.Windows.Forms.DataVisualization.Charting.Chart
      $MemoryUsageChart1.Width = 800
      $MemoryUsageChart1.Height = 400
      $MemoryUsageChart1.BackColor = [System.Drawing.Color]::White

      #header
      [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.ChartAreas.Add($chartarea)
      [void]$MemoryUsageChart1.Series.Add("data1")
      $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)
      $MemoryUsageChart1.SaveImage("D:\images\Physical_Memory_Usage.png","png")

  • #58921
    Profile photo of Olaf Soyk
    Olaf Soyk
    Participant

    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
      Profile photo of M1981
      M1981
      Participant

      Okay. Thank you.

  • #58971
    Profile photo of M1981
    M1981
    Participant

    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?

    clear-host
    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"
    $msg.To.Add("cbz@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
    $msg.Attachments.Add($att)
    $smtp.Send($msg)
    $att.Dispose()
    Invoke-Expression "d:\images\Report.html"

    Thanks in advance.

  • #58975
    Profile photo of M1981
    M1981
    Participant

    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.Attachments.Add($attachment)
    $msg = new-object Net.Mail.MailMessage
    $msg.From = "abc@gmail.com"
    $msg.To.Add("cbz@gmail.com")
    $msg.IsBodyHTML = $true
    $msg.Body = $body
    $msg.Subject = "Image from PowerShell " + (get-date -format yyyyMMdd)
    $smtp.Send($msg)

You must be logged in to reply to this topic.