Prof. Powershell
Checking Your Homework
Last week's assignment: Create a formatted report of some processes with the powers of PowerShell. How did you do?
- By Jeffery Hicks
- 01/27/2009
Last week I gave you a PowerShell assignment: Create a formatted report of the top 10 processes where the peak paged memory is greater than 10MB. For each process, select its process id, name, paged memory, peak paged memory, working set and peak working set sizes. You should be able to write a single PowerShell command to accomplish this task. I hope you realize that you'll need to send objects from one cmdlet to another through the pipeline.
Were you successful? Here's one possible solution. Remember, this is one long PowerShell command.
PS C:\> get-process | where {$_.peakpagedmemorysize -gt 10mb} | sort PeakPagedMemorySize -descending | select -first 10 | Format-Table Id,Name,PagedMemorySize,PeakPagedMemorySize,WorkingSet,PeakWorkingSet –auto
Hopefully you got something like this. If you piped Get-Process to Get-Member you should have found the properties necessary to complete the assignment. These are properties that don't necessarily show up when you simply run Get-Process, but they are available if you know their name.
The output of Get-Process is piped to Where-Object which only keeps objects with a peakPagedMemorysize greather than 10MB. You could have used 10485760 or 10000000 but the memory shortcut gets you bonus points. The remaining objects are then piped to Sort-Object which sorts the objects on the PeakPagedMemorySize and writes the objects to the pipeline in descending order.
The sorted objects are then piped to Select-Object which only keeps the first 10. These 10 objects are then piped to Format-Table which only displays the specified properties. I use the –autoformat parameter to create a report with tidy and well adjusted columns.
Give yourself 20 points for each section of the pipelined expression that you got right. Prof. PowerShell grades on a straight percentage.
How'd you do? If you didn't do so great, please keep up with the Prof. PowerShell columns and maybe start working your way through Windows
PowerShell:TFM (2nd. Ed.).
Class will be back next week with a new lesson.
About the Author
Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.