My Approach to the 2012 PowerShell Scripting Games Beginner Event #1

The details of the event scenario and the design points for Beginner Event #1 of the 2012 PowerShell Scripting Games can be found on the “Hey, Scripting Guys! Blog”.

Listed below are my notes about the requirements and design points:
Computers run Windows 7 and servers run Windows 2008 R2. They’re all in one domain.
PowerShell remoting is enabled on all computers (both servers and desktops).
Use PowerShell to retrieve the top ten processes based on the memory working set.
There’s no need to write a script. The command can be a one liner. You can use aliases.
The command must be capable of running remotely against other domain computers.
Your command should return an object.
You should be able to write the results to a file if required.

I know I’ll have to use Get-Process so I started out by just running that cmdlet on my local computer with no parameters to determine what results are returned by default:

I want the top ten results based off of Working Set. I initially missed this and sorted by Virtual Memory. The screenshot included in the requirement details for event #1 on the Scripting Games site clued me into re-reading the requirements again and catching this. Take your time and read thoroughly. WS(K) is probably the Working Set, but let’s pipe Get-Process to Get-Member to see what the available properties are:

I can see that WS is an alias to a property named “WorkingSet”. I’ll use the property “WorkingSet” since I’ve always heard if you’re providing a command or script to someone else that you shouldn’t use aliases.

In order to get the top ten processes based on WorkingSet, I’ll need to first sort by that property in descending order. Pipe Get-Process to Sort-Object to accomplish that. My first sort was in exactly the wrong order so I ran Get-Help Sort-Object -Full and determined that there was a -Descending parameter that would sort as required.

Now that I have it sorted correctly, I can select the first (top) ten. Pipe Sort-Object to Select-Object. Run Get-Help Select-Object -Full to determine what the available parameters are. There’s a -First parameter and specifying 10 meets the requirements.

I have what I need as far as the local computer goes. I could simply run it against remote computers using the –ComputerName parameter of Get-Process, but I couldn’t resist using PowerShell remoting since the scenario stated it was enabled on all computers.

Using PowerShell remoting also gives me a column that tells me what computer the results are from where using the –ComputerName parameter of Get-Process wouldn’t and the results wouldn’t mean much if it was run against multiple computers. Here’s a link to the one liner that I submitted for this event on the PowerShell Code Repository site.

Something that was confusing to me about this particular event was that the command should return an object and another point was writing the results to a file. If you add the code to pipe this to a file doesn’t that keep it from outputting an object?

µ

1 Comment

  1. CraigJ

    Nice Post, I had almost the exact script as you.

    Reply

Leave a Reply

%d bloggers like this: