2012 Scripting Games Advanced Event 1: Review a Coworker’s Script


 2012 Scripting Games badge

Summary: Advanced Event 1 of the 2012 Scripting Games asks you to review a coworker’s script.

About this event

Division

Advanced

Date of Event

4/2/2012 12:01 AM

Due Date

4/9/2012 12:01 AM

 

Event scenario

You work as a network administrator for a large company. You are part of a team of other network administrators that monitor several mission critical applications. Your coworker has convinced your boss that he knows everything about Windows PowerShell. As a result, whenever any scripting needs to be done, he always gets the nod. Recently, however, your boss has begun to have some doubts, and the boss has asked you to do a code review of the script that is shown here:

MonitorNotepadFor10Seconds.ps1

$notepad = Get-Process notepad

for ($i = 1 ; $i -le 10 ; $i++)

{

 start-sleep 1

 $notepad

}

When you run the script, you receive the output that is shown here:

Image of command output

Design points

  • If no changes are required to the script, put “No changes” in your entry for this event.
  • If changes are required:
    • Make the minimum number of changes to the script. Points will be deducted if you make too many changes.
    • Consider the performance of the script when you make changes.

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 (53)

  1. Anonymous says:

    @K_Schulte ha, this does seem like a trick question.  

  2. K_Schulte says:

    @Eager troll: That's what I mean!

    It is always possible that the boss did ask my coworker to do exacly that, what the script does! So in this case: there is nothing to correct in the script … we should correct our boss!

    Klaus

  3. Anonymous says:

    K_Schulte, no worries. It was the link above to "All Links on One Page" which was broken, at least for me. 🙂

  4. jrv says:

    @IamMred – I am very sorry.   I thought that event had clo

  5. K_Schulte says:

    @Neptune443: You should read the article
    blogs.technet.com/…/how-to-register-for-the-2012-powershell-scripting-games.aspx

    I’m afraid you have to choose only one category … beginner vs. advanced

    Klaus

  6. K_Schulte says:

    @Alex: that's one reason, why I'm stuck and I have been asking similar questions.

  7. mredwilson says:

    @Geathan.A you cannot delete a submission once it is entered. Send me an email to Scripter@MIcrosoft.com and let me know which category you will compete in. Then tommorow, and the following days, just submit one script in the category you decide you want to compete.

  8. K_Schulte says:

    Hi Ed,

    this is not too enlightening!

    I n´know it might be stupid … but: If the boss wants to have exactly this kind of information, my coworker has scripted … its OK! If the task has been any different … it's not OK!

    So how can we know without a task description? Even the most stupid script may be OK, if it solves the job description!

    No way … Klaus

  9. Anonymous says:

    This isn't too clear on what exactly the boss wanted results on.

  10. mredwilson says:

    @K_Schulte the boss wants you to review the code written by a co-worker. If the script is good, then submit "No Changes" for your event submission. If a change is required, then make the MINIMUM number of changes to permit the script to work correctly. This event, is a troubleshooting event. It is not about best practices, or code design, but what is required (if anything) to make the script work properly.

  11. K_Schulte says:

    Hi Ed,

    the games have begun! WOW!!!!

    And this short first advanced event already might knock me out … 🙂

    I'm reading through this event over and over again and I really have no clue how to solve it having no knowledge of the concrete job definition behind it!

    Could you clarify the task, the boss has been asking for, please!?

    Klaus.

  12. mredwilson says:

    @BobMule @GMFaust yes, you are only allowed to submit for either advanced or for beginner. Keep in mind that once you submit for one (either beginner or advanced) you should continue only submitting in that same category. You could, at least in theory, submit the advanced today, the beginner tommorow, and so forth, but it would not give you a good score because all advanced entries are totaled and all beginner entries are totaled. See the FAQ blogs.technet.com/…/faq-for-the-powershell-2012-scripting-games.aspx

  13. mredwilson says:

    Please do not post answers to these questions in the comments. This event is still live. It is not fair to the ones who have already worked hard and submitted their scripts!

  14. Anonymous says:

    How do I submit my Scripting Games 2012 entries? I've tried to go to the Scripting Games 2012 page but get an Access Denied error? What's happening? Also can I submit in both Beginner and Advanced?

  15. mredwilson says:

    @Oyvink Kallstad Awesome!

  16. mredwilson says:

    @JRV I did not intend for this event to be a trick question, merely a bit deceptive 🙂

  17. mredwilson says:

    @Neptune443 you will need to register. Once you have done that, you go to the same URL, LogIn, and submit your entry. See this article for information about registering blogs.technet.com/…/how-to-register-for-the-2012-powershell-scripting-games.aspx

  18. K_Schulte says:

    And now for something completely different…

    Did you ever notice that the Hey Scripting Guys! articles maybe sorted by Most recent / – Views / – Comments?

    What do you think will be the top article if you sort by "most comments"? 🙂

    WOW! We have created a "from zero to 1st place phenomenon" with this this 1st event of the 2012 scripting games!

    Klaus.

  19. jrv says:

    I missed it but now I see.

    It was a real trick question.  How long is one second?

  20. K_Schulte says:

    @Øyvind @Bob: Thaks for replying to my problem!

    Maybe … it really should be sufficient to take it easy and write the answer down …

    But depending on the concrete task description, my answer could vary 🙂

    Anyway: Good luck to everybody!

    Klaus.

  21. Anonymous says:

    Part of being an advanced scripter is the ability to make decisions based on the information provided. In this scenario, your boss is counting on YOU to review the script, not vice versa.

  22. Anonymous says:

    @IamMred How can we delete a submission if we submitted both today?

  23. K_Schulte says:

    @Øyvind @Bob: Thaks for replying to my problem!

    Maybe … it really should be sufficient to take it easy and write the answer down …

    But depending on the concrete task description, my answer could vary 🙂

    Anyway: Good luck to everybody!

    Klaus.

  24. Fábio Jr says:

    And if the notepad is not running? I have to display a message?

  25. jrv says:

    @IamMred – I have no idea where I got the idea that the events were only for one day. I almost didnd't enter because it seemed to place to much of a rush condiion on the entries.  I am glad I was wrong.

  26. mredwilson says:

    @marcadamcarter dont be too literal. In fact, I do not think that the design guidelines even said monitor for only 10 seconds, but you are on track because it was the name of the script that the (rather dim witted) coworker wrote.

  27. mredwilson says:

    @JAS I do not require that the judges actually run all code. In MANY cases, this is completely unnecessary. For example, many of the beginner events have a one line solution. After grading several HUNDRED of these submissions for the SAME event, I can tell you if a one line solution meets the requirements of an event or not. If I have any doubts about a solution I run the script. ALSO keep in mind that I can use copy and paste from PoshCode and run your script. Download takes longer, and so you may see number of downlaods equal to 0, and in fact you script has been run by every judge who graded it. ALSO just because your script runs without errors that does not mean that it meets all requirements for a solution. For example many events have things a script must do. These correspond to gaining a point each. Some also have things a script must not do … each of these corresponds to losing a point. If a script does not meet the outlined requirements At ALL you will only get 1 point for submitting.

  28. Anonymous says:

    You're probably trying to read too much into it Klaus, I did the same with Beginner Event 1, spent a while trying to do too much and then decided it didn't have to be as complicated as I'd first thought.

  29. mredwilson says:

    @Alex McFarland no problem. I am glad the event is beginning to "click" for you.

  30. mredwilson says:

    @K_Schulte The script is supposed to monitor the status of a running application. It does this by using Get-Process. If the application is not running an error displays. Look at the Script, and the output does the script work properly.

  31. mredwilson says:

    @Carlos Nunez I invite you to enter the 2012 Scripting Games and to post your answers there. See this page for information about the games and about registering blogs.technet.com/…/the-2012-windows-powershell-scripting-games-all-links-on-one-page.aspx

  32. mredwilson says:

    @JRV that is ok. I deleted your comment 🙂 I understand people getting caught up in the excitement of the games. Everyone, see the grid at the top of each event. You have 7 days for each submission. Take your time, read the event, and get a great score.

  33. K_Schulte says:

    @Martijn @Fábio Jr: Well martijn, this is part of our event description … yes!

    But "minimum amount of changes" to achieve …. what?

    Even though I can easily see that the script to review is not working the way, I would expect it to work … the question from Fábio Jr is something I would seriously consider to be a potential part of a minimum change ( again: to achieve "what"? )

    Klaus.

  34. mredwilson says:

    @K_Schulte that is true, but only if it really meets the needs. If there is an error in the script, then it does not matter WHAT the scenario was, because the script has a problem. If the script does not have any errors, and it appears to work properly, then as you said, it does not matter what the scenario was because even the stupidest script may be ok.

    You have boiled it down to the essential question: does the script work or not. If the script works, then you have to assume that it is what the boss wanted. Look at the name of the script, look at the output from the script. Look at the code. Is it all proper? If everything is fine, then you would tell the boss that there are "no changes" required. If there is a problem, then you will make the minimum changes required to cause the script to function properly. The task is essentially a troubleshooting task.

  35. Anonymous says:

    I didn't realise you had to choose one, I've entered event 1 for both. Unfortunately I get that access denied page when I try to go follow the rules link. 🙁

  36. mredwilson says:

    @Alex McFarland the minimum number of changes (if any). Remember, you did not write this script. You are doing a code review. You do NOT want to have to "support" the script once you are done 🙂 The boss did not ask you to re-write the script and bring it into best practices, add error handling, comment based help, turn it into a function and add it to a module, he simply asked you to do a code review — does the script work, is it returning the correct information, if NOT what is the minimum number of changes required to ensure it does work properly. If the script is ok, and does in fact work, then simply enter "no changes" as your entry.

  37. mredwilson says:

    @Fabio Jr. no, you should start notepad prior to working on the script. Remember, I am simply using notepad as an application that everyone will have. The POINT of the script is to monitor an application that is normally running (think about Exchange, or SQL) but I do not want you to have to install Exchange or SQL just to do this event 🙂 So I am using notepad as an example. If notepad is not running, or if you stop notepad during the montioring phase, I would expect an error would be generated. That is ok — that is the way the "person who knows everything about PowerShell" wrote the script. It is NOT YOUR JOB to completely rewrite his script. Your job is to ensure the script actually works. IF it already does work, then simply enter "no changes" as your submission for the event.

  38. mredwilson says:

    @BobMule you are correct, this event does NOT have to be overly complex, but it does require careful reading. What you do NOT want to do, is delete the entire script, and write a completely new one. That is not what the boss is asking. Keep in mind, you are merely doing a code review for a co-worker. DO NOT RE-Write the co-workers script, merely make as few changes (if any) that are required to permit the script to work properly. YOU may not LIKE the script, but that is not what the question asks.

  39. Here's my follow up thoughts on my previous literal point of view…

    Literally accounting for the amount of time passing during each iteration of the script block would dictate that you would also have to calculate the cumulative average time the script block execution itself took so that you could determine before each iteration if, based upon your average script block execution time, you had enough time remaining (in your 10 seconds) to attempt another iteration.  Hopefully Ed and the Judges aren't on the lookout for the Robert Frosts among us for this event.  For I've lived and fished in the mountains of Montana where taking the road less traveled will only get you eaten by a big bear.

  40. Øyvind Kallstad says:

    Klaus. I had some problems figuring this one out myself, but got it in the end. There information needed is there 😉 Good luck!

  41. Hi Ed,

    Are we talking about a true minimum number of changes, or the minimum number of changes to ensure the script is following PowerShell best practices for writing a script?

  42. Øyvind Kallstad says:

    Good question Alex. I was wondering the same thing actually.

  43. Hi Fábio Jr,

    You should make the minimum amount of changes to the script.

    Martijn.

  44. Matt Hitchcock says:

    I got caught overthinking this one as well but cracked it now 😀

  45. gmfaust says:

    Same with me, it didn't prompt me to choose from beginner or advanced. I have submitted a script for both.

  46. JoeD says:

    Hey Klaus, I think I found it.  Took me a while to see it too.  Read the name of the PS1 file.

  47. @IamMred says:

    Awesome, that's exactly what I was looking for!  It sounded a bit like the instructions were contrary to the general judging criteria, and I didn't want to shoot myself in the foot by doing something "correct" like making the code reusable.  Thank you for the clarification.

  48. @IamMred – that last comment (thanks on clarification) was from me, forgot to fill in a Name. 🙂

  49. DawnVillejoin says:

    Enjoying this thread 🙂

    I've decided to enter the Beginner's category, but I am really anxious to see what everyone has submitted for this one.  I have an opinion (personal submission in my head) :), but of course I may be really blown away when I see what everyone else has submitted.

    Ed, thanks again for having such an involved activity for everyone to participate in!

  50. Talk about over-thinking a solution…(literally)…

    Here's a new spin.  Monitor Notepad For "10 Seconds" – So I can take this to literally mean "stop" execution at 10.0000 TotalSeconds?  Or rely on the likelihood that Get-Process will take (on average) fewer than 100 milliseconds per iteration so at the end of 10 iterations of a for loop I'll still be at less than 10.99999?  Wow, yeah I don't think I'm going to wait for confirmation that I'm being too literal…[submit]

  51. Justin Stokes says:

    @IamMred Thanks for the clarifications as they definitely helped me to shift my focus. In the end though I am a little annoyed at myself for not seeing the solution yesterday.

  52. getmore4less says:

    so the fundamental problem with this script is the Get-process is assigned to a variable outside of the loop. So, each time the $notepad is displayed, it is always the same. Really, get-process should be run inside the loop.

  53. JAS says:

    Are the judges required to run the actual code presented or just read it and use their personal knowledge ? I ask because I know my code works for this event on 2 separate systems however I was given 1 star and told it would not work as designed. I responded to the judges comments with additional code that helps demonstrate that it works as designed.

Skip to main content