2012 Scripting Games Beginner Event 1: Use Windows PowerShell to Identify a Working Set of Processes


 2012 Scripting Games badge

Summary: Beginner Event 1 of the 2012 Scripting Games uses Windows PowerShell to identify a working set of processes.

About this event

Division

Beginner

Date of Event

4/2/2012 12:01 AM

Due Date

4/9/2012 12:01 AM

Event scenario

You are the network administrator for a small, single-location company. Your desktops are all running Windows 7, and your servers are all running Windows Server 2008 R2. Your company has a single domain, and Windows PowerShell remoting is enabled on all computers—both servers and desktops. Your boss is concerned because a number of users have complained that their computers are slow. This is surprising and alarming because each user received a new desktop when they were migrated to Windows 7. To better get a handle on what is going on with the desktop computers, your boss has directed you to ascertain the top ten processes that are consuming memory resources on each computer.

You decide, after doing a bit of research, that the working set of each process would be the best property to track. You also decide to use Windows PowerShell to gather the information. An acceptable output is shown in the following image.

Image of command output

Design points

  • There is no need to write a script for this event.
  • The command you use should be CAPABLE of running remotely against other computers in the domain, but you are not required to have multiple computers available for this scenario.
  • Because this command can be a one-liner, aliases are acceptable in your submission.
  • Your command should return an object that could be piped to additional commands.
  • You should be able to write the results of your command to a file if required (but writing to a file is not a requirement for this scenario).

2012 Scripting Games links

2012 Scripting Games: All Links on One Page

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. Good luck as you compete in this year’s Scripting Games. We wish you well.

Ed Wilson, Microsoft Scripting Guy 

Comments (37)

  1. Anonymous says:

    it seems so simple that i have the feeling that i misreaded the event guideline. 😛

    Well i will know at the end 😉

  2. Anonymous says:

    @Joshua Taylor – I am guessing because since you didnt use a ComptureName parameter, your script doesn't look capable of running remotely.

  3. Anonymous says:

    Submitted an answer, kind of new to Powershell so I don't know if it is correct.  It worked on my machine though, so there is that. 🙂

  4. Anonymous says:

    When I click on the 2012 Scripting Games: All Links on One Page I get an Access Denied page.

  5. mredwilson says:

    @Craig Jahnke you don't have to use invoke command as Get-Process has a computername parameter. See my above comment. The comment, if from me, was merely informational … I did not count off for using invoke-command.

  6. mredwilson says:

    @Michael Moore dont 🙂 This entry can easily be a one-liner. Keep in mind the guidelines, there is no need to write a script to do this. But, of course, it might take a decent amount of time to actually come up with that one line.

  7. Anonymous says:

    @IamMred – just curious, i got this comment on my script:  don't have to use invoke-command as get-process has a -computername parameter

    Not sure if it meant I should or shouldn't not of used it, or if it was just informational and that I didnt have to.

  8. mredwilson says:

    @Dawn Villejoin Get-Process is able to go against remote computers. It therefore meets the requirements. Is it the best solution in this scenario? Probably not, because Get-Process requires special ports to be opened in the Windows Firewall, and Windows PowerShell remoting WE KNOW is enabled, and therefore would work. I actually like the Invoke-Command answer … and probably give you a 5 on your solution. The other advantage to Invoke-Command is that it allows you to submit alternate credentials, and Get-Process requires impersonation. In the scenario I wrote, I was not worried about consolidated, or separately grouped output.

  9. Anonymous says:

    Really encouraged at how simple this was. I only picked up powershell last month, but doing this initial exercise really demonstrates how easy it is to learn.

  10. Anonymous says:

    Ed, I'm confused by the grading here.  I received 2.5 point for my solution, and the comments were as follows:

    "Decent solution. What I would suggest: this is one-liner so aliases are more than welcome. Also: would be great to see in the command/ comment/ description how do you plan to implement remote capability."

    That seems to imply I lost out on potential points because I did not use aliases, and because I didn't describe how to use it remotely.  Your design points did not say anything about that being required, only that the commands we use are capable of running remotely.

  11. mredwilson says:

    Please DO NOT post answers to these events in the blog comments. It spoils the fun for everyone involved in these games.

  12. mredwilson says:

    @DawnVillejoin no I am not penalizing for not using an alias. I completely understand your point, because most aliases do in fact make the code harder to read. Not all aliases, but certainly most of them. This is especially true for beginners.

  13. mredwilson says:

    That is the power of Windows PowerShell — it can do so much with so little code it is amazing.

  14. mredwilson says:

    @Dawn Villejoin at the PowerShell console, I often use positional parameters (there are also positional aliases such as CN for computername) because it makes the commands easier to read and to understand. In a script I ALWAYS use the complete parameter name because I want it to be perfectly clear.

  15. Anonymous says:

    Agreed. Me and a colleague just swapped our submissions. Now ive seen just how simple it can be and still fulfill all the requirements im kicking myself.

  16. mredwilson says:

    @Srikanth I flagged your second entry for Beginner Event1 as a duplicate. It's grading will not enter into your final score. It should also be deleted (hopefully, but Joel is really swamped right now).

  17. mredwilson says:

    @Dawn Villejoin Some aliases I nearly always use at the PowerShell prompt: cls, dir, md, sl, h, r, %. Aliases, once learned, can make the powershell console faster to use. Some aliases I hate, and refuse to use: Foreach for example. In a script, I NEVER use any aliases because I consider them a bad practice — lots of reasons here.

  18. mredwilson says:

    @Dawn Villejoin by the way, I hate your second one-liner with all the aliases, it is hard to read … But I would not have counted off if you had used it … I might have commented … but I would not have counted off in this event.

  19. mredwilson says:

    @Erunama Hmmmm ….. if you have 2.5 points, then it means that you have received at least two grades. One better, and one not as good from the judges. The judges all have their own ideas of what is a good script, and what is not. The rules say that aliases are acceptable, not that they are required. Often a judge will mention things (I know that I do) in the comment that is NOT directly related to the actual grade I give. I want to give hints that might make you a better scripter. For me, when I am grading, if the design says an alias is acceptable, that means you will NOT loose points for using them, but you do not gain points for using them either. I have added that very comment, that the command could be shorter by using aliases, but it did not affect my specific grade. The grading is this: the command should be capable of running remoting = 1 point. The command should return an object = 1 point. You should be able to write to a file = 1 point. The script should work and meet the requirement of the scenario = 2 points. So there are your five points. You lose a point or two points if you miss any one of these things. Keep in mind, the judges all have their own ideas, I have given them guidelines, but they are all free to express their own judgement.

  20. mredwilson says:

    @Wilso010 You need to logon and set up your account. See this article blogs.technet.com/…/how-to-register-for-the-2012-powershell-scripting-games.aspx

  21. Michael Moore says:

    It is very tempting to over complicate this one in my opinion. 🙂

  22. @Michael Moore says:

    You are so right!

    Of course there's a balance to be struck. To create a more useful and re-usable, IE long term/production oriented solution might involve a bit of extra complexity. Sometimes just a little more effort can pay dividends – as long as you know when to stop! 🙂

  23. So cool. My VBscripting skills feels litle offended.. So simple, but yet so powerfull.

  24. Michael Moore says:

    @IamMred Didn't, I went with the kiss method (Keep It Simple Stupid).  Very much looking forward to the next 9. 🙂

  25. Michael Moore says:

    @jlsuperman Same here, I just started learning it at the end of February. Such an incredibly powerful tool.

  26. Killian says:

    I've just started to learn Powershell and put an answer together for this fairly easily.  Will be interesting to see if people can make it even more compact.

    Looking forward to Exercise 2 now!

  27. Ed Wilson says:

    @Killian that is in fact, one of my favorite things about the Scripting Games, is seeing other peoples solutions. You will have to wait one week before you will be able to see other peoples answers … for event two, you only have a few more hours to wait. I wish you luck in the games!

  28. Ken Hamilton says:

    I have to say that when all I hear lately is that aliases should be avoided for hand-over/readability reasons I am surprised that this has become an exam design point – especially in the beginners section. I guess you could argue this because it is supposed to be a throw away one-liner but I would argue that tab completion would be a better skill to promote.

  29. Ed Wilson says:

    @Ken Hamilton I said that aliases are acceptable, not aliases are mandatory 🙂 You are confusing two different things. This question is all about quickly performing an interactive task. The "Aliases are bad" debate is specifically about production scripts — not even about quick scripts. Aliases are not bad, they are a great way to quickly come up with code, and to minimize typing. For example, ps is two key strokes. Get-p<tab> is six key strokes. As long as you know that ps is an alias for Get-Process it is much faster (three times) less typing.

  30. DawnVillejoin says:

    @erunama @IamMred Regarding alias use: I'm really hoping the other judges don't penalize for NOT using an alias (unless, of course, the guidelines would specifically say "use aliases").

    I completely understand the concept behind it, but I just cannot make myself use them in something that will be available "in print" online for new Powershell users to read. When I'm sitting at my desk, running quick one-liners to gather info, or even in a small script that I reuse often, it's Alias Invaders!

    I'm fairly new to Powershell and I still read things online that raise an eyebrow. But in print, it still looks "neater" to have the cmdlet typed out.

    Just saying… 🙂

  31. srikanth says:

    Somehow, 2 entries have been submitted by me for this ( both scripts the same ), kindly ignore any 1 of them.

  32. Joshua Taylor says:

    I am curious why my Event 1 script got 3 stars due to not spelling out parameters while another script, which abbreviated considerably more gets 5 stars.

  33. Dawn Villejoin says:

    More of an observation than a question… After reviewing some of the event 1 entries, I've noticed that some have used "Get-Process | sort WS -Descending | Select -First 10"

    Detail Point: The command you use should be CAPABLE of running remotely against other computers in the domain, but you are not required to have multiple computers available for this scenario.

    Scenario comment:  "Your boss has directed you to ascertain the top ten processes that are consuming memory resources on EACH computer."

    When using "Get-Process" against multiple computers, ie, Get-Process -ComputerName comp1,comp2…., it returns the top ten processes collectively from the set of computers, not EACH computer, not meeting the event requirement.

    Also, since the scenario states that remoting has been enabled across the domain, why not make use of that?

    Invoke-Command -ComputerName comp1,comp2 { Get-Process | Sort WorkingSet -Descending | Select -First 10 }

    or for the ones who enjoy aliases in one-liners:

    icm -cn comp1,comp2 { ps | sort ws -des | select -f 10 }

    This command actually returns the TOP TEN processes from EACH computer; and as an added bonus, and for logging purposes, it also returns a PSComputerName property 🙂

    **Of course, this one-liner is an assumption of mine (and I didn't include the computername parameter in the actual command, just in the usage comments)… I scored 3.5 on event 1 and could be way off base!!

    I hope I don't come across as a "know it all" or something of the sort… farthest from the truth!  I really just enjoy learning about Powershell and what it can do AND have fun while learning!!  Who knew, huh?

    Anyway, I just wanted to make that comment 🙂

  34. Dawn Villejoin says:

    @IamMred  As do I… I only put it there because so many judges are commenting to use more aliases…  And it took me longer to look up some of the aliases than it would have taken to just type it out (even less time to use Tab Completion in Powershell)! 🙂

  35. Dawn Villejoin says:

    @IamMred  agreed!

  36. Dawn Villejoin says:

    @IamMred  "Never use aliases in a script" I get… But in your opinion, what is the best practice for positional parameters? OK to leave out or should those be included?

  37. @Craig Jahnke says:

    I figured that had something to do with it. I figured I misinterpreted 'Capable' as the command itself IS capable of running against a remote computer, either by adding the -ComputerName parameter or Invoke-Command. I assumed the deduction was related to the judge's comment. Truth be told, this has been one of my biggest frustrations with the scripting games. If I get 5 stars I know that I've met all the requirements, but if a judge is going to deduct stars it would be incredibly helpful to know why. It does me know good to tell me that I've done something incorrectly and not tell me what.

Skip to main content