2012 Scripting Games Beginner Event 6: Compute Uptime for Local Computer

 2012 Scripting Games badge

Summary: In Beginner Event 6, you are required to compute the uptime for the local computer.

About this event



Date of Event

4/9/2012 12:01 AM

Due Date

4/16/2012 12:01 AM 

Event scenario

You are the main network administrator for a small company. As such, your duties consist of a variety of functions, including assisting the Help Desk when they get behind. Recently, your manager has become concerned about server uptime. He wants you to write a script that will display how long a server has been “up.” He said that he is not concerned with anything fancy—he just wants a general idea. The only real guidance he provided for the task is, “Use WMI. There is a class called Win32_OperatingSystem that should do the trick for you. Tell me the server name, and how many days, hours, and minutes the server has been up.”

The output that is shown in the image that follows meets the boss’s requirements.

Image of command output

Design points

  • Your solution only needs to run locally.
  • Your script should be easy to read and easy to understand.
  • Your script is only concerned with the uptime for the local server, calculated with the current time. Do not worry about time zones or about comparing up times with different computers.
  • Pay attention to the formatting of the output and the script itself.

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

  1. Anonymous says:

    I think I've figured this one out but after getting 1 star on one that I thought I had nailed I think I'll sleep on it and check again tomorrow. Triple check is my new mantra.

  2. mredwilson says:

    @Daniel-D 24 hour format will be fine. I do not expect you to calculate AM / PM.

  3. mredwilson says:

    @Mr Killian Wow … hmmmm… let me see. Use Get-Member and look at both the properties and the methods. You might also want to look at the Hey Scripting Guy Blog to see about working with WMI dates. There may be some links in the study guides for the Scripting Games. I have study guides on the All in One page for several years of Scripting Games. There may be a section about working with dates there.

  4. mredwilson says:

    @Chris-D This is good advice, double check everything and make sure you are happy with your script. I would also add that you have 7 days before an event becomes due, and so you can set it aside for a little bit. Also be careful when submitting the script — make sure you are submitting it for the correct event!

  5. mredwilson says:

    @Matt Tilford Yes, you are right the boss only asked for minutes, but the output also includes seconds. It is not a requirement, but I threw it in there because I already had the value. You would not lose a point for including extra information — but you would lose points if your solution does not include the minutes.

  6. Anonymous says:

    I'm really struggling on this one.  Think I've found the property I need to use but it's a string and I can't figure out how to make it more friendly and able to go into the equation I want it to.  Any advice?

  7. Anonymous says:

    Quick clarifying question : By uptime you mean how much time the server has been up since it rebooted? Or how much time in general?

  8. mredwilson says:

    @Eleftheria Th time since last reboot.

  9. Anonymous says:

    By "Up" time, do you mean how long has it currently been running?  Or do you mean how long has it been "Up" for the life of the server?  I am guessing by the sample output you just mean currently running, but thought I should double check.

  10. mredwilson says:

    @Jlsuperman the boss specified that you should use win32_operatingsystem, so that makes it a requiement.

  11. Chris-D says:

    This one wasn't too difficult. 10 seconds after I submitted my script, I realized I should have done something different that might cause me a point. I guess in the future I will make sure I am super happy with my final script before submitting. Double check everything, and make sure you are completely happy with your scripts.

  12. ckrull says:

    I am kicking myself, I had it all done and just noticed something is amiss. I didnt see it since I normally dont have it set to this value.

  13. Matt Tilford says:

    Just to confirm, the boss asks for minutes but the script output shows seconds. Which should we be using?

  14. James Stallwood says:

    Wow went off on a tangent on this one and it was getting really complicated before I changed direction and found a much easier way to do this!

  15. jlsuperman says:

    The design points don't mention actually using the win32_operatingsystem. If there's a way around it, can we use that or is it one of the requirements of the script?

  16. Daniel-D says:

    I see my Get-Date is returning 24-hour time when converted to string (no AM/PM). Would I lose a point for not formating to the 12 hour time with AM/PM per your sample output? You mention paying attention to the formatting of the output, but it seems to me it would add unneeded work to format the date for 12hour AM/PM.

  17. Chris Seiter says:

    Last year I got dinged a star for simplicity for using the correct tense of the noun/verb combination when the number associated with it was 1, ie, "there is 1 minute" rather than "there are one minutes".  I didn't see any simplicity requirements on this one but I don't want to let Mrs. Rabe down.  Those rulers hurt!

Skip to main content