2012 Scripting Games Advanced Event 2: Find Information about Remote and Local Services

 2012 Scripting Games badge

Summary: In Advanced Event 2, you must find information about remote and local services.

About this event



Date of Event

4/3/2012 12:01 AM

Due Date

4/10/2012 12:01 AM

Event scenario

You are a server administrator for a medium-sized enterprise company. Your boss has tasked you with finding the way that all services on all servers start. You need to present your findings in a spreadsheet that includes the following information:

  • The server name
  • The service name
  • The start mode of the service
  • The user account used to start the service
  • The current status of the service

An acceptable output for this task is shown in the following image.

Image of command output

Design points

  • You should use the easiest method possible to display the information in a spreadsheet.
  • You must include the ability to run the script both on a local computer and on a remote computer.
  • You must include the ability to run the script with alternate credentials when operating against a remote computer—you should impersonate the logged on user when running locally.
  • You do not need to add comment-based Help for this scenario, but additional points are awarded if you do include appropriate comment-based Help, such as a description, examples, and parameters.
  • If the script requires administrative rights to run, you should check to ensure that the script is running with admin rights. If those rights are not present, you should display an appropriate message and exit.
  • For the purpose of THIS exercise, do not write a module. You should make your script stand completely alone, with NO external dependencies. Therefore, EVERYTHING this script needs should be put in this script. Failure to do so will cost you points.

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

  1. mredwilson says:

    @State vs. Status Yes, I was deliberately ambiguious. There is only one property that acturately returns whether a service is running or stopped. You should find, and use that property. Points will be deducted if you do not use the correct property, or if you do not actually return whether the service is running, paused, or stopped.

  2. mredwilson says:

    @Sean I can certainly see your confusion, but in reality it is easier than you think. All your script needs to be able to do is to HANDLE a remote connection to a remote computer. You can satisfy this requirement by using a cmdlet that supports making a remote connection to a computer. In the computername, you have several options. MAKE CERTAIN that what you type will WORK when I run it on my computer. See Handling the default condition for ideas on how to do this. Here is a link to that article blogs.technet.com/…/create-and-use-default-values-in-powershell-scripts.aspx

  3. mredwilson says:

    @Chris Seiter Take a look at my Hey Scripting Guy blog posts. I have numerous posts about comment based help. In fact, I have a function in my ISE Profile that automatically adds help to a script. blogs.technet.com/…/searchresults.aspx

  4. mredwilson says:

    @MarcAdamCarter In the ISE, there are three panes (at least in version 2.0). The bottom pane is an interactive window. When I write scripts in the ISE, I often create an advanced function. Then I use the immediate pane to test my advanced function, therefore there is NO line of code at the bottom of the script that automatically produces output. The judges are smart enough to see this, and to grade accordingly. What I do NOT like is a submission that is an inline script, that requires me to save it out as a different file, navigate to that location, and then fun the script with various command line parameters from the PowerShell console. This is especially a lot of work AND it reduces the reusability of the code. Scenario 1 is truely the best from both a good PowerShell style, as well as from grading in the Games.

  5. mredwilson says:

    @Matt Hitchcock Yes, it is true that there are different ways the script could be running and have admin rights … your task is to determine if the script has the rights it requires to successfully complete PRIOR to running the script and having it fail. If your script requires specific rights to run (either locally or remotely) then you must see if the script has those rights prior to execution of the script, and gracefully exit before displaying a bunch of access denied errors. The boss did not specify HOW you are to determine that you are querying all of your servers. Therefore it is up to you, you can read a text file, query AD, query a SQL Database, provide an prompt and type the server names in, whatever is appropriate for you. But keep in mind, that your script also must have NO external dependencies, and that judges will be running your script for score … (the judges might not have Active Directory installed on their network, or they might not have a SQL database on their laptop) …

  6. mredwilson says:

    @Alex McFarland yes, you can assume that PowerShell Remoting is enabled.

  7. mredwilson says:

    @K_Schulte yes, it is a managable task 🙂 Yes, you can rely upon Excel being installed on the local computer where the report will be displayed. The picture above, shows a spreadsheet, and states that the output is acceptible. So it is safe to assume that the data is able to be displayed by Excel.

  8. Hello Ed/Judges,

    One of the things that always comes out (for me) is my lack of experience/expertise with best practice on handing off a PowerShell script.  Starting with this event, I've been dealing with issues on the best way to write the script so that 1) it functions as intended for the judge and 2) retains the best functionality with minimal effort on my part.

    From my vantage point I have 3 options (please set me straight O' grand scripting pooh-bah):

    Script Type 1: Advanced Function


    function foo{…}

    Script Type 2: Adv Function with call to function


    function foo{…}

    foo -params

    Script Type 3:


    <script block>

    (I want to preface my explanation by saying that I typically live in ISE and rarely open the console.)

    Script Type 1 – Not typically how I would write a script for myself but…regardless it provides me with functionality to load the script in the console session and then call the script by function.  

    Caveats: Won't run if copied into ISE without adding call to function at the end of the script.  Guessing this example would get me zero stars.

    Script Type 2 – This is how I write scripts, in the ISE, typically for myself, with a call to the function at the bottom of the script.  I'll modify the function call as needed and the click the F5 key to execute.

    Caveats: Loaded into the console session would depreciate the functionality of the script due to the function call within the script.  Script would run as is but you wouldn't be able to modify -parameters for the function.

    Script Type 3 – If I have a need to accomplish a task quickly and no foreseeable need to reuse or handover a script I'll write in this style (no advanced functionality however).  I would think this would most likely accommodate more judges because copy & paste would work regardless of the environment you executed in.  

    Caveats: It wouldn't include or force me to add sleek or neat features of PowerShell which (to me) are key parts of the learning process during scripting games.  But it almost guarantees that my script will fire as expected.

    Hope I explained my dilemma adequately and you'll be able to set me straight so I can start earning those 5 stars for more than just my good looks!


  9. Ed – Thanks!

    How ironic that after moving to the amazing features that come with ISE of PowerShell v3 that I learn about some of the cool functionality of v2.  Third pane is usually the first thing I hide after setting execution policy.  Scripting Games truly help me become a better scripter.

  10. mredwilson says:

    @MarcAdamCarter Awesome! This is exactly why I do the Scripting games each year, to share the joy I have in working with this amazing technology. I am glad you are learning new things! WooHOO!!!

  11. mredwilson says:

    @Josh Feierman yes, you can assume that firewall ports on the remote machines are open. I did not want to require the complexity of the firewall into the scenario. So assume that Remoting works, and that the Firewall allows you to communicate.

  12. mredwilson says:

    @Ryan Ries … no idea. I did not know this page was live yet. I will ask Joel.

  13. When I browse 2012sg.poshcode.org/…/TopUsers, how is it that some people have 4 and 5 scripts rated already, even though only two events have been released so far?

  14. mredwilson says:

    @Ken Hamilton Yes, you should determine if your script requires admin rights to retreive all the service information specified by the scenario on both a local and on a remote computer. If this task requires admin rights, then you should add the code to detect if the script possesses such rights while it is running. If the script does not have admin rights, then you should display a message and exit the script.

  15. mredwilson says:

    @Jeff Schulman I think that it is save to assume that name resolution works. If you are concerned, you can always use test-connection to ensure that you can connect prior to doing the "work" of the script.

  16. mredwilson says:

    @Scott yes, we are assuming that only one server per script runs. You do not need to worry about creating separate files based upon server name and date ran.

  17. K_Schulte says:

    Hi Ed,

    this looks ( at least on first sight 🙂 like a managable task!

    What should be done, is definitely clearer than the first event, if I got it right …

    Anyway: A question I got on mind is: "May we rely on Excel being normally installed on the local machine where the report should be displayed?"

    btw: "Is it Ok to assume, the data is consumed by Excel at all?"


  18. Ken Hamilton says:

    re point: "If the script requires administrative rights to run…"

    Is this something we need to determine and only add code if necessary?

  19. Matt Hitchcock says:

    Bit stuck on this one. There are a few ways that the account could have administrator rights, or in this scenario would the account be a member of the local administrators group?  Also, "Your boss has tasked you with finding the way that all services on all servers start", does this mean an input file of server names is required or should the script discover server objects in AD?  Or is it up to us? 🙂


  20. Chris Seiter says:

    Time to look at last year's submissions for 'comment-based help' help and another small novel for the notes section of it.  If my thinking doesn't get fixed then my scripts will never get fixed.

  21. Hi Ed,

    May we assume that PowerShell remoting is enabled?

  22. Would making the assumption that DNS or WINS is running within the environment count as an external dependency?

  23. Josh Feierman says:

    Hi there,

    The question about remoting led me to another question: can we assume that any / all firewall ports to remote machines are open, or could there be restrictions there?


  24. State vs. Status says:

    I see that the top bullet point identifies "The current status of the service", while the spreadsheet display shows the "state" of the service. These are both valid data points for a system service. Is this a purposeful ambiguity? Will points be deducted for returning both or either?

  25. Scott says:

    Are we assuming only one server per script run?  If not, do we want to store the server's data in seperate files?

  26. Sean says:

    I'm having difficulty understanding the requirements for handling multiple servers as I'm seeing what appears to be contradictory directives.

    From problem description: Your boss has tasked you with finding the way that all services on all servers start.

    From one response: The boss did not specify HOW you are to determine that you are querying all of your servers. Therefore it is up to you, you can read a text file, query AD, query a SQL Database, provide an prompt and type the server names in, whatever is appropriate for you.

    From a 2nd response: @Scott yes, we are assuming that only one server per script runs. You do not need to worry about creating separate files based upon server name and date ran.

    The first two items point to a pretty clear directive to have the script handle multiple servers at once, but the reply to @Scott clearly says "only one server per script runs". These can lead to drastically different scripts in terms of complexity. You help is appreciated in clearing up my confusion